Read and write throttling and character flow of files in java

Reading and writing files in java If the abst...
Reading and writing files in java

If the abstract method in an abstract class or interface is overridden by a subclass, the method called when instantiating the subclass must be overridden

File class file operation related

Abstract representation of file and directory pathnames
Get the object of the file create the object by constructing the method
File operation has two basic functions
Create filecreatenewfile calls this method to create
Delete file delete //Boolean
Whether exists exists exists in the inventory path / / returns true if it exists

File file = new File("E:"+File.separator+"demo.txt"); if(file.exists()) {//Existence returns true file.delete();//Deletion of files }else { file.createNewFile();//File creation }

isFile determines whether the object is a path or a directory

File separator\

Separators are different according to different systems

File file = new File("E:"+File.separator+"demo.txt");
If the directory does not exist, judge whether the path exists first

1. Specifying the path of a file getParentFile returns the parent directory name of this abstract path without returning null
2. If there is no path to create the directory mkdirs, the path will be created automatically

getName get file name
Is the path given by isDirectory a folder
Is isHidden a hidden file
Last modified date
length returned in bytes

// System.out.println (file name+ file.getName()); // System.out.println("table of contents? "+ file.isDirectory()); // System.out.println("file? "+ file.isFile()); // System.out.println("hide? "+ file.isHidden()); // System.out.println("modified on" + new date( file.lastModified ())); // System.out.println (file size+ file.length());

Modify path

file.renameTo(newfile)//Cut heavy naming
List file contents listFile returns in array form to view all files under the current folder
File result[] = file.listFiles(); for(int x = 0 ; x < result.length; x++) { System.out.println(result[x]); }
If there are folders under the folder, use recursion
public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("E:"+File.separator+"myio"); print(file); } public static void print(File file) { if(file.isDirectory()) {//file is the path File result[] = file.listFiles(); if(result != null) { for(int x = 0; x < result.length; x++) { print(result[x]); } } } System.out.println(file); }
Byte stream and character stream

Operate on file content
Byte operation stream OutputStream InputStream
Character operation stream writer Reader
Close after operation

Byte output stream OutputStream abstract class must have subclass instantiation fieldoutputstream

Single byte int a set of bytes [] specifies the location to read output.write(data.getBytes(),0,3);

File file = new File("E:"+File.separator+"myio"+File.separator+"demo.txt"); if(!file.getParentFile().exists()) {//Existence returns true / / the parent path does not exist //file.delete(); / / deleting files file.getParentFile().mkdirs();//establish } OutputStream output = new FileOutputStream(file);//Create an output stream String data = "hello java"; output.write(data.getBytes());//Output data in bytes output.close();//close resource

InputStream byte input stream
read reads a single byte
Read a set of bytes
Read specified bytes

InputStream input = new FileInputStream(file); byte data[] = new byte[1024]; int len = input.read(); input.close(); System.out.println(new String(data,0,len));

Mobile SMS opens up a designated length of space to receive

InputStream input = new FileInputStream(file); byte data[] = new byte[1024]; int foot = 0;//Angle sign int temp = 0; while((temp = input.read()) != -1) { data[foot++] = (byte) temp; } input.close(); System.out.println(new String(data,0,foot));

writer character output stream
Compared with outputStream, it can output characters directly

Writer out = new FileWriter(file); String data = "hello"; out.write(data); out.close();

Reader character read stream

Reader in = new FileReader(file); char data[] = new char[1024]; int len = in.read(data); System.out.println(new String(data,0,len));

Byte stream for terminal data
Character stream for buffer

8 June 2020, 22:49 | Views: 8669

Add new comment

For adding a comment, please log in
or create account

0 comments