Construct File class
File F=new file (fileName);
Determine if the directory
F.isdirectory ();
Gets the file name under the directory
String[] Filename=f.list ();
Get the file under the directory
File[] Files=f.listfiles ();
1,Java How to read files
package com.yyb.file; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.InputStream; /* * Read the file: * 1, locate the specified file * 2, create a file based on the file input stream * 3, create a byte array * 4, read the contents, put into the byte array * 5, close the input stream */public class FileRead {p Ublic static void Main (string[] args) {//build specified file filename = new ("E:" + file.separator + "Hello.tx T "); InputStream in = null; try {//create file input stream according to file in = new FileInputStream (file); Create byte array byte[] data = new byte[1024]; Read the contents and put it into the byte array in.read (data); System.out.println (new String (data)); } catch (Exception e) {e.printstacktrace (); } finally {try {//Close input stream in.close (); } catch (Exception e) {e.printstacktrace (); } } } }
2, Java How to write files
Package com.yyb.file; Import Java.io.File; Import Java.io.FileOutputStream; Import Java.io.OutputStream; / * * Write to File: * 1, locate the specified file * 2, the output stream according to file creation * 3, convert content to byte array * 4, write content to file * 5, close input stream */ public class FileWriter {public static void Main (string[] args) { //build specified file filename = new ("E:" + Fi Le.separator + "Hello.txt"); OutputStream out = null; try { //The output stream of the file created according to the file out = new FileOutputStream (file); String message = "I am a good man." "; Convert content to byte array byte[] data = Message.getbytes (); Writes content out.write (data) to a file, } catch (Exception e) { e.printstacktrace (); } finally { try { //Close output stream out.close (); } catch (Exception e) { e.printstacktrace () }}}
3. How to copy files in Java
<span style= "FONT-SIZE:18PX;" >package Com.yyb.file; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import Java.io.InputStream; Import Java.io.OutputStream; */* Implementation ideas: * 1, build source files and Target files * 2, the source file creates an input stream, the target file creates an output stream * 3, creates a byte array * 4, uses a loop, the source file reads part of the content, the target file writes part of the content, until all content is written * 5, close the source file input stream File output stream */public class FileCopy {public static void main (string[] args) {//build source file = New File ("E:" + file.separator + "HelloWorld.txt"); Build target file FileCopy = new file ("D:" + file.separator + "HelloWorld"); InputStream in = null; OutputStream out = null; try {//destination file does not exist to create if (! ( Filecopy.exists ())) {filecopy.createnewfile ()); }//source file Create input stream in = new FileInputStream (file); The destination file creates an output stream out = new FileOutputStream (fileCopy, true); Create byte array byte[]temp = new byte[1024]; int length = 0; The source file reads part of the content while (length = in.read (temp))! =-1) {//target file writes part of the content OUT.W Rite (temp, 0, length); }} catch (Exception e) {e.printstacktrace (); } finally {try {///close file input/output stream in.close (); Out.close (); } catch (Exception e) {e.printstacktrace (); }}}}</span><span style= "FONT-SIZE:24PX;" > </span>
Io stream file read, write, and copy in Java