[Android study notes] JAVA basics - character stream and file reading and writing

The advantage of using character streams: instead of creating a very large buf, the file is divided into many small parts for reading and writing.

Create two txt, from and to, write text in from
import java.io.*;//Import all IO classes in java
class Test{
	public static void main(String args[]){
		FileInputStream fis = null;//Declare the input stream reference
		FileOutputStream fos = null;//Declare the output stream reference
		try{
			// Generate an object representing the input stream
			fis = new FileInputStream("d:/java/src/from.txt");
			// Generate an object representing the output stream
			fos = new FileOutputStream("d:/java/src/to.txt");
			
			byte[] buffer = new byte[100];//Generate a 1K byte array
			while(true)//It will be executed without finishing reading
			{
				//Call the read method of the input stream object to read the data in from.txt
				int temp_length =fis.read(buffer,0,buffer.length);
				if(temp_length == -1)//Return -1 to indicate that the reading is completed and jump out directly
					break;
				//Call the write method of the output stream object to write data to to.txt			
				fos.write(buffer,0,temp_length);
				String s = new String(buffer););//Restore byte to character
				//Call the trim method to remove the leading and trailing spaces and null characters from the string
				//For example, "abc def" will become "abc def" after using trim
				s.trim();
				
				System.out.println(s);//Print out				
			}


		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			//Add a layer of try to catch an error when closing the input and output streams
/*
Test.java:27: error: unreported exception error IOException; must be caught or declared in order to be thrown
                        fis.close();
                                 ^
Test.java:28: error: unreported exception error IOException; must be caught or declared in order to be thrown
                        fos.close();
*/			
			try{
				fis.close();//Close the input stream
				fos.close();//Close the output stream				
			}
			catch(Exception e){
				System.out.println(e);
			}


		}
	}
}
The above is the way to read and write the byte stream
//********************************Gorgeous dividing line***** ****************************//
Character stream: When reading and writing files, it is based on characters.

Byte input stream: Reader, Common subclass FilerReader
common subclass method
int read(char [] c, int off, int len);
byte output: Writer, common subclass FilerWriter
void write(char [] c, int off, int len);

code Basically similar to a byte stream
import java.io. *;
class Test{
	public static void main(String args[]){
		FileReader fr =null;
		FileWriter fw = null;
		try{
			fr = new FileReader("d:/java/src/from.txt");
			fw = new FileWriter("d:/java/src/to.txt");
			char [] buffer = new char[1024];
			while(true)
			{
				int temp_length = fr.read(buffer,0,buffer.length);
				if(temp_length == -1) break;
				fw.write(buffer,0,temp_length);
//				String s =new String(buffer);
//				s = s.trim();
				System.out.println(s);			
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		finally
		{
			try{
				fr.close();
				fw.close();			
			}
			catch(Exception e){
				System.out.println(e);				
			}
		}
	}
}

By Urien April 4, 2018 15:48:39



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325858934&siteId=291194637