Discussion on Java IO stream operation file

Source: Internet
Author: User

1. Task description

2. Read a character

3. Read the entire file

4. Copying files

5. Copying large files

6. Use character stream to solve garbled problem

I. Task description

Everyone knows that the program is run without memory, but a lot of things are not in the program, and the program needs to manipulate them. So we're going to need something to deal with out-of-memory resources, and we'll call this an IO stream. The IO stream is divided into the output stream and the input stream, where the output stream is output to memory, and the input stream reads something out of memory into memory. Once an IO stream is available, it is convenient to deal with resources outside of memory. Why do you say that, in fact, the overall only need three steps, the first step, positioning the data source, the second step, the establishment of pipelines, the third step, the operation of the pipeline. For the first step, if you are manipulating the resources above the hard disk

Package test;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

/**
*
* @author User
*
*/
public class myFileStream {
public static void Main (string[] args) {
Locating data sources
File File = new file ("D:\\liu.txt");
File file1 =new file ("D:\\liu1.txt");
Create a byte stream pipeline
try {
FileInputStream fis = new FileInputStream (file);
FileOutputStream ops=new FileOutputStream (file1);


Operation Pipeline
int length = fis.available ();
for (int i = 0; i < length; i++) {
System.out.println ((char) fis.read ());
Ops.write (Fis.read ());
}
} catch (FileNotFoundException e) {

E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Package test;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintWriter;
/**
* about the operation of reading files
* 1. It is best to use less filereader character stream to read the file, because he is decoding with the default system character encoding
* Workaround:
* You can use the input character stream inputstreamreader instead of FileReader; he can specify character encoding.
* 2. How to efficiently traverse the entire text file
* You can use the buffered stream BufferedReader stream to read one line at a time.
* @author User
*
*/
public class FileOprate2 {
public static void Main (string[] args) {
File F=new file ("D:\\liu.txt");
File F1=new file ("D:\\liu1.txt");
Char[] Chs=new char[1024];
try {
InputStreamReader insr=new InputStreamReader (new FileInputStream (f), "utf-8");
OutputStreamWriter osw=new OutputStreamWriter (new FileOutputStream (F1), "Utf-8");
PrintWriter pw=new PrintWriter (OSW);
BufferedReader br=new BufferedReader (INSR);
while (Br.ready ()) {
System.out.println (Br.readline ());
Pw.write (Br.readline ());
}
Pw.close ();

} catch (FileNotFoundException e) {

E.printstacktrace ();
} catch (IOException e) {

E.printstacktrace ();
}
}
}


Discussion on Java IO stream operation file

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.