!!!!
Read the contents of a txt file
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- /**
- * May 11, 2016
- * This class simulates the process of reading content from a txt file
- * @author WuJieJecket
- *
- */
- public class PrintFromTxt {
- public static void main(String[] args) throws Exception {
- //read all file contents
- File file = new File("d:\\a\\123.txt");
- StringBuilder sb = new StringBuilder();
- String s ="";
- BufferedReader br = new BufferedReader(new FileReader(file));
- while( (s = br.readLine()) != null) {
- sb.append(s + "\n");
- }
- br.close();
- String str = sb.toString();
- System.out.println(str);
- }
- }
! ! ! write to file - if the specified file does not exist, it will be created
Use if in the main method to judge whether to run the test method. There are four test methods. Both FileOutputStream and FileWriter are written to txt files. The difference is that the latter is written in the cache first, and the cache is cleared or the cache is full. The content is put into txt.
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- /**
- * May 11, 2016
- * This class simulates the process of writing strings to txt files
- * @author WuJieJecket
- *
- */
- public class WriteToTxt {
- public static void main(String[] args) throws FileNotFoundException {
- //Content to be written to the file \r is a newline
- String str1= "\rZhang San 1 0 3000\rLi Si 1 1 5000\rWang Wu 1 0 4000";
- String str2= "\rZhang San 2 0 3000\rLi Si 2 1 5000\rWang Wu 2 0 4000";
- String str3= "\rZhang San 3 0 3000\rLi Si 3 1 5000\rWang Wu 3 0 4000";
- String str4= "\rZhang San 4 0 3000\rLi Si 4 1 5000\rWang Wu 4 0 4000";
- WriteToTxt wtt=new WriteToTxt();
- //FileWriter-cache write-character-overwrite rewrite
- if(!true){
- wtt.FileWriterTest(str1);
- }
- //FileOutputStream-direct write-byte-overwrite rewrite
- if(!true){
- wtt.FileOutputStreamTest(str2);
- }
- //FileWriter-cache write-character-end continuation
- if(!true){
- wtt.FileWriterTest2(str3);
- }
- //FileOutputStream-direct write-byte-end continuation
- if(!true){
- wtt.FileOutputStreamTest2(str4);
- }
- }
- /*
- * write file
- * Overwrite
- * FileWriter
- * Write in the cache first, need to flush
- * character eg:1,2,a,b
- */
- public void FileWriterTest(String str){
- FileWriter writer;
- try {
- // writer = new FileWriter("/home/1.txt");
- writer = new FileWriter("d:\\a\\1231.txt");
- writer.write(str);
- writer.flush(); //This one is more important, it is the process of clearing the cache, and then the information to be written is written to the txt file
- writer.close();
- } catch (IOException e) {
- e.printStackTrace ();
- }
- }
- /*
- * write file
- * Override rewrite
- *FileOutputStream
- *Write directly to the file, there is no cache
- *byte byte
- */
- public void FileOutputStreamTest(String str){
- FileOutputStream fos;
- try {
- fos=new FileOutputStream("d:\\a\\1232.txt");
- fos.write(str.getBytes());
- fos.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace ();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace ();
- }
- }
- /*
- * write file
- * Continued at the end
- * FileWriter
- * Write in the cache first, need to flush
- * character eg:1,2,a,b
- */
- public void FileWriterTest2(String str){
- FileWriter writer;
- try {
- // writer = new FileWriter("/home/1.txt");
- writer = new FileWriter("d:\\a\\1233.txt",true);
- writer.write(str);
- writer.flush(); //This one is more important, it is the process of clearing the cache, and then the information to be written is written to the txt file
- writer.close();
- } catch (IOException e) {
- e.printStackTrace ();
- }
- }
- /*
- * write file
- *Continued at the end
- *FileOutputStream
- *Write directly to the file, there is no cache
- *byte byte
- */
- public void FileOutputStreamTest2(String str){
- FileOutputStream fos;
- try {
- fos=new FileOutputStream("d:\\a\\1234.txt",true);
- fos.write(str.getBytes());
- fos.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace ();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace ();
- }
- }
- }
txt read by line and column
http://blog.csdn.net/bestcxx/article/details/65446489
Finally, a packaged method written by myself is attached, which can be used directly and will not overwrite the original file (ie, continue at the end)
package com.zhaowu.renwu2; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class FileUtil { public static void toFile (String content) { File file = null; FileWriter fw = null; file = new File("/home/acer/桌面/aaa"); try { if (!file.exists()) { file.createNewFile(); } fw = new FileWriter(file,true); fw.write(content); // Copy the content to the file fw.flush(); } catch (IOException e) { e.printStackTrace (); }finally{ if(fw != null){ try { fw.close(); } catch (IOException e) { e.printStackTrace (); } } } } }