Java io Stream Knowledge point
package myio;
Import Java.io.File;
Import java.io.IOException;
Import Org.junit.Test;
/**
* The simplest of the file
*
* @author Administrator
*
*/
public class Test1 {
@Test
public void T1 () {
String Parentpath = "e:/wallpaper";
String name = "123.jpg";
Relative path Build file src = new file (parentpath, name);
System.out.println (Src.getname ());
System.out.println (Src.getpath ());
Absolute path File SRC2 = new file ("e:/wallpaper/123.jpg");
System.out.println (Src2.getname ());
System.out.println (Src2.getpath ());
There is no letter with the current workspace as the root File src3 = new File ("A.txt");
System.out.println (Src3.getname ());
If it is an absolute path, return the absolute path if the relative path System.out.println (Src3.getpath ());
Absolute path System.out.println (Src3.getabsolutepath ());
Get the top level directory System.out.println (Src.getparent ());
System.out.println (Src2.getparent ());
A folder without a letter creation returns a null SYSTEM.OUT.PRINTLN (Src3.getparent ());
/** * Judgment Information * * @Test public void T2 () {File File = new file ("A.txt");
File src = new file ("e:/wallpaper/123.jpg");
SYSTEM.OUT.PRINTLN ("Determine whether the file exists:" + file.exists ());
Boolean canread = Src.canread ();
Boolean canwrite = File.canwrite (); System.out.priNtln ("canread=" + canread);
System.out.println ("canwrite=" + canwrite);
System.out.println ("is the folder:" + src.isdirectory ());
if (Src.isfile ()) {System.out.println ("file");
else {//folder or file does not exist if (src.isdirectory ()) {System.out.println ("folder");
}else{System.out.println ("file does not exist");
}//File bytes can only read the System.out.println of the file folder (Src.length ()); /** * Create and delete files * * @throws IOException/@Test public void T3 () throws IOException {file src = new file ("e:/wall
Paper/12345.jpg ");
if (!src.exists ()) {//does not exist, the creation file exists to return false Boolean flag = Src.createnewfile ();
SYSTEM.OUT.PRINTLN (flag);
///delete file Boolean deleteflag = Src.delete ();
System.out.println (Deleteflag);
Create temporary files file = file.createtempfile ("Test", ". Temp", New file ("e:/wallpaper"));
Exit is to delete file.deleteonexit ();
}}
Package Myio;
Import Java.io.File;
Import Java.io.FileFilter;
Import Org.junit.Test;
/**
* Operation Directory
*
* @author Administrator
*
*/
public class Test2 {
/**
* Create Folder
*/
@Test
public void T1 () {
String path = "e:/wallpaper/123456";
File File = new file (path);
Create a folder if the previous parent directory exists
Boolean a = File.mkdir ();
System.out.println (a);
File File2 = new file ("e:/wallpaper/123/456/678");
Created together if the parent directory chain does not exist
Boolean B = File2.mkdirs ();
System.out.println (b);
}
/**
* Get the contents of the folder * *
@Test public
void T2 () {
String path = "e:/wallpaper";
File File = new file (path);
if (File.isdirectory ()) {
//Get all file name folder names in the folder also have
string[] fileNames = File.list ();
for (string string:filenames) {
System.out.println (string);
}
System.out.println ("-------Split line------");
Gets all the file path folder paths in the folder path also has
file[] fil = file.listfiles ();
for (File f:fil) {
System.out.println (F.getabsolutepath ());}}}
}
Package Myio;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Org.junit.Test;
/**
* file read First
*
* @author Administrator
*
/
public class Test3 {
@Test
Publi c void T1 () {
//file to read
String path = "e:/wallpaper/a.txt";
File File = new file (path);
//Select stream
InputStream ins = null;
try {
ins = new FileInputStream (file);
//define a byte array
byte[] by = new BYTE[1];
int len = 0;
/**
* A little small don't understand this place wants new string how many times can you connect? But can output the test with
* True to read text file with ObjectInputStream line read
/
int a = 0;
while (len = Ins.read (by))!=-1) {
a++;
String str = new string (by, 0, Len);
System.out.println (str);
}
System.out.println ("a==" + a);
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
//close stream
if (ins!= null) {try {ins.close ();
catch (IOException e) {e.printstacktrace ();
/** * Writes the simplest test of the output stream in the file * * @throws IOException/@Test public void T2 () throws IOException {
String path = "e:/wallpaper";
Originally this file is not exist first create it and then write to the inside of something file File = new file (path, "a.txt");
Select stream OutputStream Ops = new FileOutputStream (file); String str = "Qiangdi Why blame Willow, Spring breeze is not the Yumen guan."
";
Write byte[] byt = Str.getbytes ();
Below two the same//Ops.write (byt,0,byt.length);
Ops.write (BYT);
Refresh the Ops.flush ();
Close Ops.close (); /** * Copy of * * * * * * @throws IOException/@Test public void T3 () throws IOException {//source file must exist or error file fi
Lea = new File ("e:/wallpaper/a.txt");
Directory file directory files can be non-existent file Fileb = new file ("e:/wallpaper/bbb.txt");
///If the file exists but is a folder if (Fileb.isdirectory ()) {fileb.delete (); ///If not present create file if (!fileb.exists (){//Fileb.mkdirs ()//error This is to create a folder Fileb.createnewfile ()//Create a file}*/inputstream ins = new Filein
Putstream (Filea);
OutputStream Ops = new FileOutputStream (Fileb);
/** * This set too large but the file too small problems do not know the impact does not affect * * byte[] byt = new byte[1024];
int len = 0;
One reads a write while (len = Ins.read (byt))!=-1) {ops.write (byt);
/** * Flush written in the inside good or outside * * * Ops.flush ();
Close flow ops.close ();
Ins.close ();
}
}
Package Myio;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Org.junit.Test;
/**
* Copy folder to use the recursive test with no use to encapsulate the tool class so easy to see
*
* @author Administrator
*
*/
public class Test4 {
public static void Main (string[] args) throws IOException {File src = new file ("e:/wallpaper");
File Dest = new file ("E:/test");
file[] filelist = Src.listfiles (); for (File files:filelist) {if (Files.isdirectory ()) {//Create a change path in the destination folder Dest = new file (d
EST, src.getname ());
Here the two methods have the same effect AS//Dest.mkdir ();
Dest.mkdirs ();
T1 (files,dest);
}else{Files.createnewfile ();
} T2 (files, dest); } public static void T1 (file Src,file dest) throws IOException {}/** * Copy only single file src source file dest target file * * @throws IOE Xception */public static void T2 (file src, file dest) throws IOException {InputStream ins = new FileInputStream (src
);
OutputStream Ops = new FileOutputStream (dest);
int len = 0;
byte[] byt = new byte[1024];
while (len = Ins.read (byt))!=-1) {ops.write (byt);
} ops.flush ();
Ops.close (); Ins.close ();
}
}
Package Myio;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.Reader;
Import Java.io.Writer;
Import Org.junit.Test;
The
/**
* Character stream operation character stream can only process text files. txt. HTML
*
* @author Administrator
*
/
public class Test5 {/**
* Read First test for character streams
*
* @throws ioexception
/
@Test
public void T1 () throws IOException {
File File = new file ("e:/wallpaper/a.txt");
//Convert stream character to byte this is the trouble for using the BufferedReader method
//Decoding settings character Set
BufferedReader reader = new BufferedReader (New Inputstre Amreader (
New FileInputStream (file), "Utf-8"));
//character stream used array if char array
//Here Set too general problem
/*
* char[] cha = new char[1023]; while (Reader.read (CHA)!=-1) {St Ring
* str = new String (CHA); System.out.println (str);
/
///new method with wrapper class read one line at a time
//byte throttling wrapper class does not provide a new method
String s = null;
while ((s = reader.readline ())!= null) {
String str = new String (s);
System.out.println (str);
}
Reader.close ();
}
The copy//test character stream for a single file of a character stream is
not used in this way it should be serialized
@Test public
void T2 () throws IOException {
file Filea = new file ( "e:/wallpaper/a.txt");
File Fileb = new file ("e:/wallpaper/b.txt");
Reader reader = new FileReader (Filea);
Writer Writer = new FileWriter (Fileb);
/**
* Here set too large seemingly problematic
/int len=0;
char[] cha = new char[1024*3];
while (Len=reader.read (CHA)!=-1) {//
suggestions are as follows
///commented out on the existence of multiple or less readable questions
//writer.write (cha);
Writer.write (Cha,0,len);
}
Writer.flush ();
Reader.close ();
Writer.close ();
}
}
Package Myio;
Import java.io.UnsupportedEncodingException;
/**
* Encoding and decoding
*
* @author Administrator
*
*/
public class Test6 {
public static void M Ain (string[] args) throws unsupportedencodingexception {
String str = "small programmer";
//encoding to binary
//encoding and decoding it is best to set the character set
byte[] byt = str.getbytes ("Utf-8");
//decode
String s = new string (byt, "utf-8");
System.out.println (s);
}
}