JAVA (basic grammar)

Verbatim https://www.dazhuanlan.com/2019/08/25/5d622b7041013/


Stick

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class {

public static void main(String[] args) {
try {

File a =new File("D:/file1.txt");
File b= new File("D:/file3.txt");
FileInputStream c = new FileInputStream(a);
FileOutputStream d = new FileOutputStream(b);
int e;
while((e= c.read())!= -1 ){
d.write(e);
}
c.close();
d.close();
}catch(FileNotFoundException e) {
System.out.println("FileStreamsTest:" + e);
}catch(IOException e){
System.err.println("FileStreamTest:" + e);
}
}
}

Writing document data in a byte stream of

try{
File f = new File("C:\Users\Administrator\Desktop\1.txt");
FileOutputStream f1 = new FileOutputStream(f);
byte [] a ={};
f1.write(a);
f1.close();
}
catch(IOException e){
e.printStackTrace();
}

First create a document object, and destinations. Then create objects out of a document, the two linked, then speak

Create and delete documents

File f1 = new File("D:\1.txt");
File f2 = new File("D:\","2.txt");
File f3 = new File("D:\",File.separator+"1.txt");
f2.createNewFile();
f3.delete();

copy and paste

the try { 
File f1 = new new File ( "D: \ 3.txt" );
File F2 = new new File ( "5.txt" );
FileInputStream in = new new FileInputStream (f1); // create an input stream, from a disk write memory
a FileOutputStream OUT = new new a FileOutputStream (F2); // create an output stream, from a disk memory write int C; the while (! (C = in.read ()) = - . 1 ) { out.write (C); } in.close (); the out.close (); } the catch (IOException E) { e.printStackTrace (); }









Read character information

try{
FileReader file = new FileReader("D:\3.txt");
int data = 0;
while((data= file.read())!=-1){
System.out.print((char)data);
}
file.close();
}catch(IOException e){
e.printStackTrace();
}

Delete Document

Files.delete(Paths.get("D:\4.txt"));

View Document Information

File a=new File("D:\4.txt");
System.out.println(a.getName());
System.out.println(a.getParentFile());
System.out.println(a.getPath());

Guess you like

Origin www.cnblogs.com/petewell/p/11408052.html