[Java] File-type file management and IO read/write and copy operations

Source: Internet
Author: User
Document directory
  • Summary of the file class:
  • Files and folders
  • Writer writes files
  • Reader reads files
  • File copy operations
  • Download source code:
Summary of the file class:

1. create files and folders

2. File Reading

3. File writing

4. File Replication (Bytes stream, byte stream, and processing Stream)

5. Download the image at the image address

File and folder related functions (Boolean) mkdir () Create the directory specified by this abstract path name
(Boolean) mkdirs () creates the directory specified by this abstract path name, including all the parent directories that are required but do not exist.
(Boolean) Delete () delete the file or directory represented by this abstract path name
(Boolean) createnewfile () creates an empty file when the file with the specified name does not exist.
Create a file
Public static void newfile (string pathstring) {file = new file (pathstring); If (! File. exists () {try {If (file. createnewfile () {system. out. println ("File Created successfully") ;}} catch (exception e) {// todo: handle has tione. printstacktrace () ;}} else {system. out. println ("file already exists ");}}

Create a folder

Public static void newfilebox (string pathstring) {file file2 = new file (pathstring); If (! File2.exists () {If (file2.mkdirs () {system. out. println ("folder succeeded") ;}} else {system. out. println ("folder exists"); file2.delete (); // destroy file }}

Application:

public static void main(String[] args) {NewFile("test/file.txt");NewFileBox("test/a/a/a/a");}

Writer writes files into filewriter

public  static void ForFileWriter(String string,String fileName) {File file = new File(fileName);try {FileWriter fWriter = new FileWriter(file);fWriter.write(string);fWriter.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}

Use bufferedwriter to write files

public static void ForBufferedWriter(String string,String desFile) {BufferedWriter bWriter = null;try {bWriter = new BufferedWriter(new FileWriter(new File(desFile)));bWriter.write(string.toString());bWriter.close();} catch (Exception e) {e.printStackTrace();}}

Application:

Public static void main (string [] ARGs) {forfilewriter ("write files with filewriter", "test/writer1.txt"); forbufferedwriter ("write files with bufferedwriter ", "Test/writer2.txt ");}

Reader reads files with filereader

Public static void testreadbyreader (string filename) {file = new file (filename); filereader FD = NULL; try {FD = new filereader (File ); char [] arr = new char [1024*1000*6]; int Len = fiis. read (ARR); string data = new string (ARR, 0, Len); FCM. close (); system. out. println (the file content read by filereader in Filename + "is \ n" + data);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}

Use fileinputstream to read files

Public static void testreadbyinputstream (string filename) {file = new file (filename); fileinputstream FD = NULL; try {FD = new fileinputstream (File ); byte [] arr = new byte [1024*1000*6]; int Len = fiis. read (ARR); string data = new string (ARR, 0, Len); FCM. close (); system. out. the file content read by fileinputstream in println (filename + "is \ n" + data);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();}}

Read files with bufferedreader

Public static void testreadbybufferedreader (string filename) {bufferedreader breader = NULL; string line = NULL; stringbuffer buffer = new stringbuffer (); try {breader = new bufferedreader (New filereader (new file (filename); While (line = breader. readline ())! = NULL) {buffer. append (line ). append ("\ n") ;}} catch (exception e) {// todo: handle has tione. printstacktrace ();} system. out. println (filename + "the file content read by bufferedreader is: \ n" + buffer. tostring ());}

Application:

Public static void main (string [] ARGs) {testreadbyinputstream ("Res/.txt"); testreadbyreader ("Res/.txt"); testreadbybufferedreader ("Res/.txt ");}

Copy an object using the copy object method.

public static void FileCopy1(String readfile,String writeFile) {try {FileReader input = new FileReader(readfile);FileWriter output = new FileWriter(writeFile);int read = input.read();while ( read != -1 ) {output.write(read);read = input.read();}input.close();output.close();} catch (IOException e) {System.out.println(e);}}
Byte stream Replication
public static void FileCopy2(String readfile,String writeFile) {try {FileInputStream input = new FileInputStream(readfile);FileOutputStream output = new FileOutputStream(writeFile);int read = input.read();while ( read != -1 ) {output.write(read);read = input.read();}input.close();output.close();} catch (IOException e) {System.out.println(e);}}

Process Stream Replication

public static void FileCopy3(String readfile,String writeFile) {BufferedReader bReader = null;BufferedWriter bWriter = null;String line = null; try {bReader = new BufferedReader(new FileReader(new File(readfile)));bWriter = new BufferedWriter(new FileWriter(new File(writeFile)));while ((line = bReader.readLine())!=null) {bWriter.write(line);bWriter.newLine();}bWriter.close();bReader.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}

Application:

Public static void main (string [] ARGs) {filecopy1 ("Res/.txt", "test/1.txt"); filecopy2 (" Res/ .txt "," test/2.txt "); filecopy3 ("Res/.txt", "test/3.txt"); filecopy2 (" Res/me.jpg "," test/33.jpg ");}

Source code download: http://download.csdn.net/detail/oyuntaolianwu/5804447

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.