Package Com.weixuan.test;import java.io.file;/** * Learn file-related operations the file class can only complete access to files and file metadata (referring to the file's attribute information) * If you want to read and write the contents of the file, Need to use FileInputStream, FileOutputStream or Randomaccessfile * * @author Nicholas * */public class Filetest {public static void Main (string[] args) {//1, must have a file object before using File File = new file ("D:\\centos-7.0-1406-x86_64-dvd.iso") ;//2, detection file existence Boolean isexists = File.exists (); System.out.println (isexists);//3, file length long length = File.length (); System.out.println (length);//4, rename or move file Boolean issuccess = File.renameto (new file ("Test2.iso")); System.out.println (issuccess);//5, delete file Boolean isdelete = File.delete (); System.out.println (isdelete);//6, the detection of a path is a file or directory Boolean isdirectory = File.isdirectory (); System.out.println (isdirectory);//7, read the list of files in the directory file File2 = new file ("C:\\users\\weixu_000\\pictures"); string[] filename = file2.list (); for (String files:filename) System.out.println (files);}}
Package Com.weixuan.test;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.IOException Import java.io.inputstream;import java.io.outputstream;/** * Use FileInputStream and fileoutputstream to manipulate files * * @author Nicholas * * FileOutputStream can write a byte stream to a file (that is, from memory output, write to file) * FileInputStream can read the contents of the file in the form of a byte stream (read from the file, write to memory) */ public class FileTest2 {private static outputstream outputstream;private static InputStream inputstream;public static Voi D main (string[] args) {try {//true means append file, false means overwrite outputstream = new FileOutputStream ("d:\\javawork\\javaiotest\\ FileoutputStream.txt ", false);//byte[] C = {' H ', ' e ', ' l ', ' l ', ' o ', ' w ', ' O ', ' r ', ' L ', ' d '};outputstream.write (" Hellow Orld ". GetBytes ());/** * When you write data to FileOutputStream, it is possible that the data will be cached in memory. * At some later time, for example, only x data can be written each time, or the FileOutputStream is closed, it will be actually written to disk. * When FileOutputStream is not turned off and you want to make sure that the data written to FileOutputStream is written to disk, * You can call the flush () method, This method guarantees that all data written to FileOutputStream will be written to disk. */outputstream.flush (); Outputstream.close ();} CAtch (IOException e) {e.printstacktrace ();} Read the file's byte stream try {inputstream = new FileInputStream ("D:\\javawork\\javaiotest\\fileoutputstream.txt"); int data = Inputstream.read (); while (data! = 1) {System.out.println (Inputstream.read ());d ATA = Inputstream.read ();} Inputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}
Package Com.weixuan.test;import Java.io.filereader;import Java.io.filewriter;import java.io.ioexception;import Java.io.reader;import java.io.writer;/** * FileReader and FileWriter * * @author Nicholas * */public class FileTest3 {public static void main (string[] args) {try {reader reader = new FileReader ("D:\\javawork\\javaiotest\\fileout PutStream.txt "); int data = Reader.read (); while (data! = 1) {System.out.print (char) data);d ATA = Reader.read ();} Reader.close ();} catch (IOException e) {e.printstacktrace ();} try {writer writer = new FileWriter ("D:\\javawork\\javaiotest\\fileoutputstream.txt");//write directly to the string Writer.write ("Hello This is Test "); Writer.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Pipeline-based thread communication
Package Com.weixuan.test;import Java.io.ioexception;import Java.io.pipedinputstream;import java.io.pipedoutputstream;/** * The pipeline in Java IO provides the ability to communicate with two threads running in the same JVM. So pipelines can also be used as data sources as well as target media. * The Java pipeline differs from the pipeline in the Unix/linux system. In Unix/linux, * Two processes running in different address spaces can communicate through a pipeline. In Java, both sides of the communication should be different threads running in the same process. * * A pipedinputstream stream should be associated with a pipedoutputstream stream. * Data written by one thread through PipedOutputStream can be read by another thread through the associated PipedInputStream. * * Remember that when you use two associated pipe flows, be sure to assign them to different threads. * the Read () method and the Write () method call can cause a stream to block, which means that if you try to read and write at the same time in one thread, it can cause thread deadlock. * * @author Nicholas * */public class Pipedtest {public static void main (string[] args) throws IOException {final Pipedo Utputstream PipedOutputStream = new PipedOutputStream ();//Pipedoutputstream.connect (PipedInputStream);// You can call PipedOutputStream initialization, or you can call connect associated final PipedInputStream PipedInputStream = new PipedInputStream ( PipedOutputStream); Thread thread1 = new Thread (new Runnable () {@Overridepublic void run () {try {pipedoutputstream.write ("This is Thread1 ' INP UT! ". GetBytes ());p ipedoutputstReam.close ();} catch (IOException e) {e.printstacktrace ();}}}); Thread thread2 = new Thread (new Runnable () {@Overridepublic void run () {try {int data = Pipedinputstream.read (); while (dat A! =-1) {System.out.print (char) data);d ATA = Pipedinputstream.read ();} Pipedinputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}); Thread1.start (); Thread2.start ();}}
Conversion of Byte stream and character stream
Package Com.weixuan.test;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstreamwriter;import Java.io.reader;import Java.io.writer;import Com.sun.jndi.url.corbaname.corbanameurlcontextfactory;public class Convert {public static void main (string[] args) {// TODO auto-generated method stub//byte converted to character file File = new file ("D:\\javawork\\javaiotest\\fileoutputstream.txt");// Character output stream writer writer = Null;try {writer = new OutputStreamWriter (new FileOutputStream (file)); Writer.write ("Hello"); Writer.flush (); Writer.close ();} catch (IOException e) {e.printstacktrace ();} Reader reader = null;try {reader = new InputStreamReader (new FileInputStream (file)); char[] C = new Char[1024];int length = Reader.read (c); System.out.println (length); Reader.close ();} catch (IOException e) {e.printstacktrace ();}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.