Java Basic-java IO operation Learning (2) __java

Source: Internet
Author: User
Tags flush string to file

Learn Java IO operation (2), write to the file, read the contents of the file.

Package Com.dufy.io;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;

Import Java.io.Writer; 
 /** * * Practice IO Operation Second <br/> * code is written, not seen, I am a punk-aflyun <br/> * on the road. * @author Aflyun * @email 742981086@qq.com * */public class Testsecondio {public static void main (string[] args) t Hrows IOException {writebytefile ();//byte stream write file Readbytefile ();//byte stream read file Changereadbytefile ();//modify byte stream read file Writestrin Gfile ();//character stream write file Readstringfile ()//character streams write file}/** * Byte throttle * 1: Write string to File * 2: Write String to a byte in file * 3: Append new content to file * @throws IOException */public static void Writebytefile () throws ioexception{String path = "E:" + File.separator + "a
	Flyun.txt ";	File File = new file (path);
		OutputStream OS = new FileOutputStream (file);
		String str = "Hello world!";
		Byte[] B = str.getbytes ();
		1: Writes a string to the file Os.write (b);
		2: Write String to byte one byte in file for (int i = 0; i < b.length; i++) {os.write (b[i)); }//3: Append new content to file String appendstr = "\ni am aflyun!";
		\ n represents a newline byte[] B1 = Appendstr.getbytes (); Os.write (B1);//Output Hello world!
		I am aflyun!
		Os.flush ();
	Os.close (); /** * Byte stream * 1: Read content * @throws IOException/public static void Readbytefile () throws ioexception{String Pat
		h = "E:" + file.separator + "Aflyun.txt";
		File File = new file (path);
		InputStream is = new FileInputStream (file); byte b[] = new byte[1024];
		Set up to 1024 bytes int len = Is.read (b);
		Is.close ();
		System.out.println (New String (b));
	SYSTEM.OUT.PRINTLN ("file reads into length:" + len); 
	 /** * Modify the above way * byte throttling * The way it was previously applied for a space of a specified size, but sometimes the space may be too small, sometimes too large, we need the exact size, this can make better use of space * @throws IOException */public static void Changereadbytefile() throws ioexception{String path = "E:" + file.separator + "Aflyun.txt";
		File File = new file (path);
		InputStream is = new FileInputStream (file); byte b[] = new byte[(int) file.length ()];
		Set the length of the file read so that it can be well set to use space//attached 1: A byte read/*for (int i = 0; i < b.length; i++) {B[i] = (byte) is.read ();
		}*///Attached 2: Determine whether the file read int tmp = 0;
		int count = 0;
		while (TMP = Is.read ())!=-1) {b[count++] = (byte) tmp;
		} is.close ();
		System.out.println (New String (b));
	
	System.out.println (count); /** * Character Stream * File write-man characters data * @throws IOException/public static void Writestringfile () throws ioexception{Stri
		ng path = "E:" + file.separator + "AflyunGood.txt";
		File File = new file (path);
		Writer out = new FileWriter (file); String str = "Hello, Yiyi."
		";
		Out.write (str);
	Out.close (); /** * Character Stream * file read * @throws ioexception/public static void Readstringfile () throws ioexception{String PA
		th = "E:" + file.separator + "AflyunGood.txt"; File File = newFile (path);
		BufferedReader bf = new BufferedReader (new FileReader (file));
		String str = "";
		while (str = Bf.readline ())!= null) {//Read System.out.println (str) by row;
	} bf.close ();
	 /** * Summary of the difference between the byte stream and the character stream * byte throttling in the operation of the time itself is not used to the buffer, is the direct operation of the file itself, but the character streams in the operation when the buffer is used, is through the buffer to manipulate the file. * Try to comment out the code for the last line of the program that closes the byte stream, and then run the program to see it.
	 You will find that there is already content in the file, but there is no content in the file when you use a byte stream, and you need to flush the buffer at this time.
	 * That's better. * The answer is byte stream. First, because all the files on the hard disk are transmitted or saved in bytes, including pictures and so on.
	 But the character only in memory will be formed, so in the development of the word throttling use a wide range.
 */
}


Related Article

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.