java txt文件输入输出

package inputtxt;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author Administrator
 * 
 */
public class WriteTxt implements Runnable {


public void run() {
List<String> list = new ArrayList<String>();
list.add("屌丝");
list.add("高富帅");
list.add("白富美");


try {
FileWriter output = new FileWriter("D://test.txt");
BufferedWriter bf = new BufferedWriter(output);
for (String l : list) {
bf.write(l + "\r\n");
}
bf.flush();// 此处很关键,如果不写该语句,是不能从缓冲区写到文件里的
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


public static void main(String[] args) {
WriteTxt rt = new WriteTxt();
Thread demo1 = new Thread(rt);
demo1.start();


// 读txt文件里的内容
try {
FileReader input = new FileReader("D://test.txt");
BufferedReader br = new BufferedReader(input);
String text = null;
while ((text = br.readLine()) != null) {
System.out.println(text);
}
br.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(java,txt,输入输出)