Java IO Learning

Source: Internet
Author: User
Tags file copy

Java IO programming refers to manipulating files through a Java program. such as file read-write, delete, backup and so on. The basic concepts of file flow need to be mastered. Master the IO programming method, grasp the byte stream and the character stream file in the program is to operate in the form of a stream what is a byte stream: The stream is bytes to read, the stream can be used to read a binary file and any type of file what is a character stream: a character stream is read as a unit of characters A character stream can read a text file. Cannot manipulate binary input: byte stream inputstream character streams reader output: byte stream OutputStream Word stream Wirter io programming--file class To do Java IO programming requires importing Java.io package import ja va.io.*;  uses the file class to get to the files object. The file class can only get to the files object and cannot read or write to the file package File_io;import java.io.*;/*** is mainly to learn Java file IO Operations, Java IO Operations need to import java.io packages containing all the Java Io class * Created by admin on 2017/8/27.*/public class Javaio {public static void main (string[] args) {//The use of the file class requires an incoming file as a parameter, Gets the file object, filename File = new file ("E:\\pythoncode\\spider\\china\\20170201152643\\iast0ic4wfw.jpg"); We'll be able to get some properties of the file//File.canexecute () returns whether the Boolean file can be executed//File.canread () returns whether the Boolean file is readable//file.canwrite () returns whether the Boolean file is writable// File.delete () Delete the file//file.exists () file exists//File.getpath () Gets the file path//File.isfile () is the file//File.isdirectory () is the directory// File.ishidden () is a hidden file//file.length () file size unit is a byte}}  creates a file and folder using the Java file class  file files = NEW file ("C:\\java.txt"); if (!file.exists ()) {//If the file is not present, create it, use the CreateNewFile method to create the//CreateNewFile method must catch exception try { Ff.createnewfile ();} catch (IOException e) {e.printstacktrace ();}}  //use the file class to create the folder file directory = new file ("C:\\io");//Create Folder if (Directory.isdirectory ()) {System.out.println (" Folder already exists ");  }else {//Create folder. If Mkdirs () is the recursive creation of the Directory Directory.mkdir ();}   Use the file class to list all files under a folder (non-recursive) file all = new file ("c:\\");//Listfiles () returns an array of File objects filelist[] = All.listfiles (); for (File files:filelist) {System.out.println (files);}  //IO Programming uses FileInputStream to read the file contents. Because the file class does not have read and write functionality. Therefore, you need to use fileinputstream file Binfile = new File ("F:\\test.txt"); FileInputStream inputstream=null;try {inputstream = new FileInputStream (binfile);//define a byte array, equivalent to the cache, to read so much data at one time, Avoid memory bursting when reading oversized files byte []bytes = new byte[1024];//defines the end for if the actual size of the file does not have our defined 1024 then the actual size is the quasi int end=0;while (end= Inputstream.read (bytes))!=-1) {string s = new String (bytes, 0, end); System.out.println (s);//System.out.write (bytes, 0, end);}} CatcH (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally {//close file try {inputstream.close ();} catch (IOException e) {e.printstacktrace ()}}  java IO programming, receives the user input data from the keyboard, writes the file. To write to a file you need to use the FileOutputStream from the keyboard to receive Y user input needs to use system.in and bufferedreader//file fileobj = new File ("C:\\object.txt"); FileOutputStream fileoutputstream=null;if (!fileobj.exists ()) {fileobj.createnewfile ();} BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (system.in)); String string = Null;string = Bufferedreader.readline (); try {//bytes output stream fileoutputstream = new FileOutputStream (fileobj);// byte []bytes = new Byte[1024];fileoutputstream.write (String.getbytes ());} catch (FileNotFoundException e) {e.printstacktrace ();} finally {if (FileOutputStream!=null) {fileoutputstream.close ();}}   java IO Programming File replication. File copy, is an input stream, an output stream. Need to be used. The file needs to be read into memory and then written to the specified location  //copy the file FileInputStream fileinputstream=null; FileOutputStream Fileoutputstream1=null; File Cpfile = new file ("D:\\sqlback\\all.sql"); File Pastefile = new file ("C:\\all.sql"), try {if (cpfile.exists ()) {FileInputStream = new FileInputStream (cpfile); Byte [] ReadByte = new Byte[1024];int fileend=0;fileoutputstream1 = new FileOutputStream (pastefile); while (fileend= Fileinputstream.read (ReadByte))!=-1) {//read to file data after writing to another location, complete replication fileoutputstream1.write (readbyte);}  }}catch (Exception e) {e.printstacktrace ();} finally {if (FileInputStream!=null) {fileinputstream.close ();} if (fileOutputStream1!=null) {fileoutputstream1.close ();}}  java IO Programming, reads the contents of a text file, writes another file. Read and write text files need to use character stream FileReader, Filewriter filereader filereader=null; FileWriter filewriter=null;try {char cache[]= new char[1024];int Readchar=0;filereader = new FileReader ("F:\\test.txt") ; fileWriter = new FileWriter ("C:\\cp-test.txt"); while ((Readchar=filereader.read (cache))!=-1) {//System.out.println (string.valueof (cache)); Filewriter.write (cache);}} catch (Exception e) {e.printstacktrace ();} finally {if (FileReader!=null) {filereader.close ();}if (FileWriter!=null) {filewriter.close ();}}  java IO programming buffer character stream. Sometimes when reading a file is relatively large. We can't read it all into memory, we need to read a bit of cache into memory at once. Therefore, you need to cache the character stream. Cache character streams are also divided into Bufferreader and bufferwriter. Direct Operation string //Bufferreader bufferwriter use BufferedReader bufferedreader1=null; BufferedWriter bufferedwriter=null;try{//bufferreader Bufferwriterfilereader fr = new FileReader ("F:\\test.txt"); BufferedReader1 = new BufferedReader (FR); FileWriter FW = new FileWriter ("C:\\okc.txt"); bufferedwriter = new BufferedWriter (fw);//loop Read string s= "";//Bufferreader The content read does not contain a terminator for any line, such as a newline character//So when you read a file written to another file, you will find that there is no newline character written, and we need to add our own while writing the file (S=bufferedreader1.readline ()) !=null) {fw.write (s+ "##$$");}  }catch (Exception e) {e.printstacktrace ();} finally {if (BufferedReader1!=null) {bufferedreader1.close ();} if (bufferedwriter! = null) {Bufferedwriter.close ();}}

Java IO Learning

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.