package com.test2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Writer;
public class Demo6 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
File file = new File("F:/test.txt");
PrintWriter pw = new PrintWriter(new FileWriter(file));
System.out.println("请输入内容:");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String str = br.readLine();
pw.println(str);
pw.flush();
pw.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Demo6.readFile();
}
public static void readFile() {
System.out.println("文件的输出");
File file = new File("F:/test.txt");
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
String str = null;
int line = 1;
try {
while ((str = br.readLine()) != null) {
System.out.println("line " + line + ": " + str);
line++;
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
文件的读写
最新推荐文章于 2024-06-17 00:30:00 发布