Read () TXT file content with write content to txt file (overwrite rewrite with +fileoutputstream and filewriter at end)

Source: Internet
Author: User
Tags flush


。。。。

Reading 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
		*/* filename = 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 is created

In the main method using if to determine whether to run the test method, the test method has four, FileOutputStream and FileWriter are written to the TXT file, the difference is that the latter is written in the cache, clear cache or cache is full before the final content 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 a string to a TXT file * @author wujiejecket * */public class Writetotxt {public static void Mai N (string[] args) throws FileNotFoundException {//content to be written to the file \ r is a newline String str1= "\ r 31 0 3000\r Lee 41 1 5000\r Harry
	     1 0 4000 ";
	     String str2= "\ r Zhang 32 0 3000\r Li 42 1 5000\r Wang 52 0 4000";
	     String str3= "\ r Zhang 33 0 3000\r Li 43 1 5000\r Wang 53 0 4000";
	     
	     String str4= "\ r Zhang 34 0 3000\r Li 44 1 5000\r Wang 54 0 4000";
	    
	     Writetotxt wtt=new writetotxt (); filewriter-Cache Write-character-overrides override if (!true) {wtt.
	     Filewritertest (STR1); }//fileoutputstream-writes directly-bytes-overrides override if (!true) {wtt.
	     Fileoutputstreamtest (STR2); }//filewriter-Cache Write-character-end continuation if (!true) {wtt.
	     FileWriterTest2 (STR3); }//fileoutputstream-write directly-byte-end continuation if (!true) {wtt. FileOutputStreamTest2 (StR4); }/* * Write file * Overwrite rewrite * FileWriter * First write in cache, need 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 is more important, is the process of cleaning up the cache, and then the information that needs to be written to the TXT file writer.close ();
	        } catch (IOException e) {e.printstacktrace (); }/* * Write file * Overwrite rewrite *fileoutputstream * Write directly to file without cache * Byte byte */public void Fileoutputstreamte
		 St (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 (); }
	 }
	 
	 
	 
	 
	 /* * * * * * * * * * * * * * * * * * * FileWriter * write in cache first, need 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 is more important, is the process of cleaning up the cache, and then the information that needs to be written to the TXT file writer.close ();
	        } catch (IOException e) {e.printstacktrace (); }/* * Write file * end of *fileoutputstream * Write directly to file, no cache * byte */public void fileoutputstreamtes
		 T2 (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 by row, column read

http://blog.csdn.net/bestcxx/article/details/65446489

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.