Usage of input byte stream (FileInputStream) and output byte stream (FileInputStream)

Source: Internet
Author: User

Import Java.io.File;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
public class Inputstreamdemo {

public static void Main (string[] args) {

File.separator represents "/", the Java virtual opportunity to convert the path delimiter according to the operating system

String info = "D:" +file.separator+ "text.txt";//Path
InputStream is = null;
try {
is = new FileInputStream (info);
Byte[] B = new byte[1024];
int temp = 0;
While ((Temp=is.read (b))!=-1) {
The following can also be written: New String (b); However, because B is 1024, if the file size is not enough 1024 will be filled with spaces
So usually we use the following method:
System.out.println (New String (b,0,temp));
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
is.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

}


public class Outputstreamdemo {
public static void Main (string[] args) {
String path = "D:" +file.separator+ "File" +file.separator+ "Aaa.txt";
File F = new file (path);
OutputStream OS = null;
String info = "This is Sunny today";
try {
F.createnewfile ();
BYTE stream
OS = new FileOutputStream (path,true);//If True means that the original content will not be overwritten, append to the text, and overwrite the original content if it does not add true
Os.write (Info.getbytes ());

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

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

}
}
}

Usage of input byte stream (FileInputStream) and output byte stream (FileInputStream)

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.