将下面的代码放在main函数里面执行一下,将大写转成小写,前提是这个文件存在D:/lin/testfile.txt
public static void ToLowerCase() throws IOException{
File file = new File("D:/lin/testfile.txt");
InputStreamReader in = new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader bufferedReader = new BufferedReader(in);
String lineTxt = null;
String test = "";
while((lineTxt = bufferedReader.readLine()) != null){
test = lineTxt.toLowerCase();
System.out.println(test);
}
FileWriter fw = new FileWriter(file);
fw.write("");
fw.close();
in.close();
OutputStream out = new FileOutputStream(file,true);
out.write(test.getBytes("GBK"));
out.close();
}
//将在控制台中输入的字符保存在txt文件中
public static void save() throws IOException{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
(new File("D:/lin/testfile.txt")).delete();
//在d盘上创建一个名为testfile的文本文件
File f = new File("D:/lin"+File.separator+"testfile.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out = new FileOutputStream(f,true);
out.write(input.getBytes("GBK"));
out.close();
}
取消
评论