Java file read/write

Source: Internet
Author: User

Read and Write files:

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


public class Dxshuju {

public static void Main (string[] args) {
String str = "Chinayang, Ajax keeps letting go, and I can't wait to see me."
+ "The right to be spoiled everyone big space hack phone card words ah ah ah ah ah ah!"
+ "The right to spoiled everybody big space hack phone card words ah ah ah ah ah ah oh \ n"
+ "The right to spoiled everyone big space hack phone card words ah ah ah ah ah ah";
Specific path to the connected file

try {
FileOutputStream fs = new FileOutputStream ("Hello.java"); Creating an Instance Object

Write a file
try {

byte [] b=new byte [5000];

B=str.getbytes ();

int off=0;

int len=b.length;

System.out.println (len);

Fs.write (b, off, Len);

System.out.println ("FS OK");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Fs.close (); Turn off the output stream
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

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

try {
FileInputStream FS1 =new fileinputstream ("Hello.java"); Create an Object association file
File f =new file ("Hello.java"); Get file properties
Long an = F.length ();
Byte[] B = new byte[(int) an];
Read the file
try {
int rEnd = Fs1.read (b); Read the whole block.
System.out.println (New String (b));

for (int i=0;i<an;i++) {

int xie = Fs1.read ();
if ( -1!=xie) {
System.out.print ((char) Xie);
}

}

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

try {
Fs1.close (); Turn off the output stream
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

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




}

}

Java file read/write

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.