Java file input/output stream learning Notes

Source: Internet
Author: User

--java Creating a file

File File=new file ("Java.txt");//At this time the Java workspace root directory does not necessarily exist java.txt

Determines whether a CreateNewFile method that does not exist that calls the file class is created Java.txt

File Files=new file ("E:\\war3");
if (!file.exists ()) {
File.createnewfile ();
}
System.out.println (File.exists ());//Determine if the file exists
System.out.println (File.isfile ());//Judging is not a file

System.out.println (Files.isdirectory ());//Judging is not a folder

System.out.println (File.getname ());//Get file name

System.out.println (File.getabsolutepath ());//Get File absolute path

If the folder is called, the Listfiles method of the file class gets its child file in the form of a collection and prints its absolute path

File[] Arry=file.listfiles ();
for (File F:arry) {
System.out.println (F.getabsolutepath ());
}

--fileinputstream input stream Read file contents
File File=new file ("Java.txt");
FileInputStream fis=new fileinputstream (file);
--Method One
int num=0;
while ((Num=fis.read ())!=-1) {
System.out.println ((char) num);
}
--Method Two
Byte[] bt=new byte[(int) file.length ()];
int num1=0;
while ((Num1=fis.read (BT))!=-1) {
String Str=new string (BT);
System.out.println (str);

--fileoutputstream output Stream Write file

File File=new file ("Java.txt");
FileOutputStream fos=new FileOutputStream (file,true);
String str= "Hello world!";
--Method One
for (int i=0;i<str.length (); i++) {
Fos.write (Str.charat (i));
}
System.out.println ("Write succeeded! ");
--Method Two
Byte[] Bt=str.getbytes ();
Fos.write (BT);
System.out.println ("Write succeeded! ");

The Fileinputstram class and the FileOutputStream class can read/write files efficiently, but for Unicode-encoded files, it is possible to use them garbled; Given that Java is a cross-platform language, it is common to manipulate Unicode-encoded files It is necessary to manipulate a file using a character stream, and using a character stream involves the following 4 classes: The >filereader class and the FileWriter class, the >bufferedreader class, and the BufferedWriter class.

--Read File information
File File=new file ("Java.txt");
FileReader fr=new filereader (file); Create a file input character stream file
BufferedReader br=new BufferedReader (FR);//create file input character stream file buffer
String str;
while ((Str=br.readline ())!=null) {
System.out.println (str);
}
--Write file
FileWriter fw=new FileWriter (file);
BufferedWriter bw=new BufferedWriter (FW);
Bw.write ("Hello word!");
Bw.newline ();
Bw.write ("Hello java!");
Bw.flush ();

Java file input/output stream learning Notes

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.