简单的java代码演讲 java有趣的简单小代码

求一段简单的java代码不知道是否理解对了你的意思 , 大概写了一下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("处理完毕");
}
/**
* 文本文件转换为指定编码的字符串
*
* @param file
*文本文件
* @param encoding
*编码类型
* @return 转换后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 将输入流写入输出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回转换结果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 将字符串写入指定文件(当指定的父路径中文件夹不存在时 , 会最大限度去创建,以保证保存成功!)
【简单的java代码演讲 java有趣的简单小代码】*
* @param res
*原字符串
* @param filePath
*文件路径
* @return 成功标记
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符缓冲区
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
JAVA程序,演讲时用的,只有几行代码,请求帮忙详细翻译一下,万分感谢public class CharEncodingFilter implements Filter {//过滤器实现类代码
public void destroy() {//析构方法
}
public void doFilter(ServletRequest request, ServletResponse response, //过滤方法
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");//将所有请求编码设为Utf-8
chain.doFilter(request, response);//处理其他过滤器
}
public void init(FilterConfig arg0) throws ServletException {

推荐阅读