Java input/output stream memo

Source: Internet
Author: User
Tags create directory sin

Important Blog:

http://blog.csdn.net/hguisu/article/details/7418161

    1. File dir = new file ("\\root"); Directory
    2. File F1 = new file (dir, "FileOne.txt"); File

Dir.mkdir ();//Create Directory

F1.createnewfile ();//Create File

All classes required for streaming I/O are included in the Java.io package. There are four basic classes in the Java.io package: InputStream, OutputStream, and reader, and writer classes that handle byte streams and character streams, respectively:

Commonly used are Bufferedinputstream and Bufferedoutputstream (buffering), FileReader and FileWriter.

5. Application of FileInputStream flow and FileOutputStream

Use the program to copy the file File1.txt to File2.txt.

[Java] View plain copy print?

Import Java.io.File;

Import java.io.IOException;

Import Java.io.FileOutputStream;

Import Java.io.FileInputStream;

public class Testfile {

public static void Main (String args[]) throws IOException {

try {

File InFile = new file ("Copy.java");

File OutFile = new file ("Copy2.java");

FileInputStream FinS = new FileInputStream (inFile);

FileOutputStream Fouts = new FileOutputStream (outFile);

int C;

while ((c = Fins.read ())! =-1) {

Fouts.write (c);

}

Fins.close ();

Fouts.close ();

} catch (IOException e) {

System.err.println ("Filestreamstest:" + e);

}

}

}

3 ) keyboard input stream read to memory

Connect BufferedReader with standard data streams

InputStreamReader sin=new InputStreamReader (system.in);

BufferedReader bin=new BufferedReader (sin);

[Java] view plain copy

Print?

  1. import java.io.*;
  2. Public class Readwritetofile {
  3. Public Static void Main (String args[]) throws IOException {
  4. InputStreamReader sin = new inputstreamreader (system.in);
  5. BufferedReader bin = new bufferedreader (sin);
  6. FileWriter out = new FileWriter ("MyFile.txt");
  7. BufferedWriter bout = new bufferedwriter (out);
  8. String s;
  9. (s = Bin.readline ()). Length () > 0) {
  10. Bout.write (S, 0, s.length ());
  11. 12.}
  12. 13.
  13. 14.}

15.}

Program Description:
Read the characters from the keyboard and write to the method of the BufferedReader class in the file: String readLine ()
Function: Reads a line of string, ending with a carriage return.
Methods of the BufferedWriter class: Bout.write (Strings,offset,len)
Function: Writes the string s from offset to the Len length string from the buffer to somewhere.

Java input/output stream memo

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.