--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