Java IO finishing (top)

Source: Internet
Author: User
Tags file copy uppercase letter

/*//Create a new file public static void main (string[] args) {file File=new file ("D:\\hello.txt"); try {file.createnewfile ();} catch (IOException e) {e.printstacktrace ();}} The two constants of the *//*//file class public static void Main (string[] args) {System.out.println (file.separator);//Result: System.out.println ( File.pathseparator);//Result::}*//*//Delete a file public static void main (string[] args) {String filename= "D:" +file.separator+ " Hello.txt "; File File=new file (fileName), if (File.exists ()) {File.delete ();} else {System.out.println ("file does not exist!) ");}} *//*//Create a folder public static void Main (string[] args) {String filename= "D:" +file.separator+ "Hello"; File File=new file (fileName); File.mkdirs ();} *//*//use Listfiles to list all files for the specified directory//* listfiles output is full path public static void main (string[] args) {String filename= "D:" + File.separator; File File=new file (fileName); File[] Str=file.listfiles (); for (int i=0;i<str.length;i++) {System.out.println (str[i]);}} *//* uses isdirectory to determine whether a specified path is a directory *//*public static void main (string[] args) {String filename= "D:" +file.separatoR File File=new file (fileName), if (File.isdirectory ()) System.out.println ("yes"), else {System.out.println ("no");}} *//* lists the entire contents of the specified directory *//*public static void main (string[] args) {String filename= "D:" +file.separator; File File=new file (fileName);p rint (file);} public static void print (File f) {if (f!=null) {if (F.isdirectory ()) {file[] filearray=f.listfiles (); if (filearray!=null) {for (int i=0;i<filearray.length;i++) {print (filearray[i]);//Recursive Call}}} else {System.out.println (f);}}} *//* using Randomaccessfile to write the file public static void main (string[] args) throws IOException {String filename= "D:" + file.separator+ "Hello.txt"; File File=new file (fileName); Randomaccessfile randomaccessfile=new randomaccessfile (file, "RW"); Randomaccessfile.writebytes ("1234567890"); Randomaccessfile.writeint; Randomaccessfile.writeboolean (true); Randomaccessfile.writechar (88); Randomaccessfile.writedouble (12.678); Randomaccessfile.writefloat (23.5f); Randomaccessfile.close ();} If you open hello at this time. TXT view, you will find that it is garbled. *//* Byte Stream * Writes a string to a file public static void MAin (string[] args) throws IOException {String filename= "D:" +file.separator+ "Hello.txt"; File File=new file (fileName); OutputStream outputstream=new fileoutputstream (file); String string= "Hello"; byte[] Bs=string.getbytes (); Outputstream.write (BS); Outputstream.close ();} *//** * Byte stream * Write string to the file in bytes of one byte * @throws ioexception * *//*public static void Main (string[] args) throws IOException {S Tring filename= "D:" +file.separator+ "Hello.txt"; File File=new file (fileName); OutputStream outputstream=new fileoutputstream (file); String string= "Hello GG"; byte[] Bs=string.getbytes (); for (int i=0;i<bs.length;i++) {outputstream.write (bs[i]);} Outputstream.close ();} *//** * Byte stream * Append new content to file: * @throws IOException * *//*public static void Main (string[] args) throws IOException {String fi Lename= "D:" +file.separator+ "Hello.txt"; File File=new file (fileName); OutputStream outputstream=new FileOutputStream (file,true); String string= "Hello MM";//string str= "\r\nrollen"; Can wrap byte[] bs=string.getbytes (); for (int i=0;i<bs. length;i++) {outputstream.write (bs[i]);} Outputstream.close ();} *//** * Byte stream * Read File contents * @throws IOException * *//*public static void Main (string[] args) throws IOException {String Filenam E= "D:" +file.separator+ "Hello.txt"; File File=new file (fileName); InputStream inputstream=new fileinputstream (file);//byte[] Bs=new byte[1024];// Pre-applied for a specified size of space byte[] bs=new byte[(int) file.length ()];inputstream.read (BS); System.out.println ("File Length:" +file.length ()); Inputstream.close (); System.out.println (New String (BS));} *//** * Byte stream * Read file * @throws IOException * *//*public static void Main (string[] args) throws IOException {String filename= " D: "+file.separator+" Hello.txt "; File File=new file (fileName); InputStream inputstream=new fileinputstream (file); byte[] Bs=new byte[1024];int count=0; int Temp=0;while ((Temp=inputstream.read ())! = (-1)) {//when reading to the end of the file returns-1. Normally, the bs[count++]= (byte) temp is not returned-1; Inputstream.close (); System.out.println (New String (BS));} *//** * Character Stream * Write data * @throws IOException * *//*public static void MaIn (string[] args) throws IOException {String filename= "D:" +file.separator+ "Hello.txt"; File File=new file (fileName); Writer out=new FileWriter (file); String string= "friend"; Out.write (string); Out.close ();} *//** * Character Stream * Read content from a file * @throws IOException * is read in a circular way, because we sometimes don't know how big the file is. * *//*public static void Main (string[] args) throws IOException {String filename= "D:" +file.separator+ "Hello.txt"; File File=new file (fileName); char[] Ch=new char[100]; Reader reader=new filereader (file), int temp=0;int count=0;while ((Temp=reader.read ())! = ( -1)) {ch[count++]= (char) Temp ;} Reader.close (); System.out.println (New String (CH, 0, Count));} */

/**
* Flow byte output to character output stream
* @throws IOException
* */
/*public static void Main (string[] args) throws IOException {
String filename= "D:" +file.separator+ "Hello.txt";
File File=new file (fileName);
Writer out=new OutputStreamWriter (new FileOutputStream (file));
Out.write ("MM");
Out.close ();
}*/

/**
* Convert byte input to character input stream
* @throws IOException
* */
/*public static void Main (string[] args) throws IOException {
String filename= "D:" +file.separator+ "Hello.txt";
File File=new file (fileName);
Reader reader=new InputStreamReader (new FileInputStream (file));
Char[] Cs=new char[100];
int Len=reader.read (CS);
System.out.println (New String (CS, 0, Len));
Reader.close ();
}*/

/**
* Convert an uppercase letter to lowercase using the content manipulation flow
* @throws IOException
* Content operation flow is generally used to generate some temporary information, so as to avoid the trouble of deletion.
* */
/*public static void Main (string[] args) throws IOException {
String str= "ADFGHJKLUYTG";
Bytearrayinputstream inputstream=new Bytearrayinputstream (Str.getbytes ());
Bytearrayoutputstream outputstream=new Bytearrayoutputstream ();
int temp=0;
while ((Temp=inputstream.read ())! = (-1)) {
Char ch= (char) temp;
Outputstream.write (Character.tolowercase (ch));
}
String outstr= outputstream.tostring ();
Inputstream.close ();
Outputstream.close ();
System.out.println (OUTSTR);
}*/

1. The difference between byte stream and character stream:

Byte stream in the operation of the time itself is not used in the buffer, is the direct operation of the file itself, but the character stream at the time of operation, is to use the buffer, is through the buffer to manipulate the file.

Is it good to use a byte stream or a character flow?

The answer is a byte stream. First, because all the files on the hard disk are transferred or saved in bytes, including images and so on. But characters are only formed in memory, so in development, the byte stream is used extensively.

2, copy of the file:

Method One: There is a file copy function under DOS

Method Two: Using the program

/** * File copy * @throws ioexception  * Idea: Read content from one file and write to another while reading. * */public static void Main (string[] args) throws IOException {String hellofilename= "D:" +file.separator+ "Hello.txt"; String rollenfilename= "D:" +file.separator+ "Rollen.txt"; File File1=new file (hellofilename); File File2=new file (rollenfilename), if (!file1.exists ()) {System.out.println ("The copied files do not exist!") "); SYSTEM.OUT.PRINTLN (1);} InputStream inputstream=new FileInputStream (file1); OutputStream outputstream=new FileOutputStream (file2); if ( inputstream!=null&&outputstream!=null) {int Temp=0;while ((Temp=inputstream.read ())! = (-1)) { Outputstream.write (temp);}} Inputstream.close (); Outputstream.close ();}

3. Pipeline Flow

The pipeline flow is primarily used for communication between two threads.

PipedOutputStream Pipeline output stream

PipedInputStream Pipeline Transmission Inflow

/* Message Send class */public class send implements Runnable{private PipedOutputStream Out=null;public send () {out=new PipedOutputStream ();} Public PipedOutputStream Getout () {return this.out;} @Overridepublic void Run () {String message= "Hello MM"; try {out.write (message.getbytes ());} catch (IOException e) { E.printstacktrace ();} try {out.close ();} catch (IOException e) {e.printstacktrace ();}}} /* Accept Message Class */public class Recive implements Runnable{private PipedInputStream input=null;public Recive () {input=new PipedInputStream ();} Public PipedInputStream GetInput () {return this.input;} @Overridepublic void Run () {byte[] bs=new byte[1000];int len=0;try {len=this.input.read (BS);} catch (IOException e) {E.PR Intstacktrace ();} try {input.close ();} catch (IOException e) {e.printstacktrace ();} System.out.println ("Received content is:" + (New String (BS, 0, Len));}} /* Test class */public static void Main (string[] args) {send send=new send (); Recive recive=new Recive (), try {send.getout (). Connect (Recive.getinput ());//Pipe connection} catch (IOException e) {E.priNtstacktrace ();} New Thread (send). Start (); new Thread (recive). Start ();}}

4. Print Flow

/* Output with PrintStream *//*public static void Main (string[] args) throws IOException {PrintStream printstream=new PrintStream (New FileOutputStream ("D:" +file.separator+ "Hello.txt"));p Rintstream.print (true); Printstream.print ("GG");p rintstream.close ();} *//* using PrintStream for Output * and formatting *//*public static void Main (string[] args) throws IOException {PrintStream printstream=new PrintStream (New FileOutputStream ("D:" +file.separator+ "Hello.txt")); String name= "GG"; int age=26;printstream.printf ("name:%s; Age:%d! ", Name,age);p rintstream.close ();} *//* uses OutputStream to output content to the screen */public static void Main (string[] args) {outputstream out=system.out;try {out.write ("MM"). GetBytes ());} catch (IOException e) {e.printstacktrace ();} try {out.close ();} catch (IOException e) {e.printstacktrace ();}}

  

Java IO finishing (top)

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.