//提示用户标准输入(键盘输入),当用户输入'end'算结束,将用户输入的信息写入到磁盘文件中。
@Test
// 字符流、缓冲区读取,并且按行读取
public void test4() {
String pathString = "E:\\doubleE\\123.txt";
File fromFile = new File(pathString);
File toFile = new File("F:\\eclipse\\123\\123.txt");
BufferedReader reader = null;
BufferedWriter writer = null;
try {
System.out.println(encodeString);
// 获得字符流的按行读取,多层嵌套包装
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile), "utf-8"));//设置编码格式,避免出现乱码
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile)));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line + "\n");
writer.write(line);
}
} catch (UnsupportedEncodingException | FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
java-提示用户标准输入(键盘输入),当用户输入‘end‘算结束,将用户输入的信息写入到磁盘文件中。
最新推荐文章于 2021-12-03 16:50:50 发布