標籤:
import java.io.*;
class hello{
public static void main(String[] args) {
System.out.println(File.separator);
System.out.println(File.pathSeparator);
}
}
【運行結果】:
\
;
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
if(f.exists()){
f.delete();
File f=new File("D:\\hello.txt");
建立新檔案
f.createNewFile();
建立新檔案夾
f.mkdir();
刪除檔案
f.delete();
列出指定目錄的全部檔案(包括隱藏檔案):
listFiles輸出的是完整路徑
String fileName="D:"+File.separator;
File f=new File(fileName);
File[] str=f.listFiles();
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
}else{
System.out.println("檔案不存在");
}
位元組流:
預先申請了一個指定大小的空間,但是有時候這個空間可能太小,有時候可能太大,我們需要準確的大小,這樣節省空間的,那麼我
們可以這樣幹
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[(int)f.length()];
for (int i = 0; i < b.length; i++) {
b[i]=(byte)in.read();
}
in.close();
System.out.println(new String(b));
}
}
有時候我們不知道檔案有多大,這種情況下,我們需要判斷是否獨到檔案的末尾
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
InputStream in=new FileInputStream(f);
byte[] b=new byte[1024];
int count =0;
int temp=0;
while((temp=in.read())!=(-1)){
b[count++]=(byte)temp;
}
in.close();
System.out.println(new String(b));
}
當讀到檔案末尾的時候會返回-1.正常情況下是不會返回-1的
字元流:
寫:
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
Writer out =new FileWriter(f);
String str="hello";
out.write(str);
out.close();
}
其實這個例子上之前的例子沒什麼區別,只是你可以直接輸入字串,而不需要你將字串轉化為位元組數組。
當你如果想問檔案中追加內容的時候,可以使用將上面的聲明out的哪一行換為:
Writer out =new FileWriter(f,true);
這樣,當你運行程式的時候,會發現檔案內容變為:
hellohello如果想在檔案中換行的話,需要使用“\r\n”
比如將str變為String str="\r\nhello";
這樣檔案追加的str的內容就會換行了。
讀:
public static void main(String[] args) throws IOException {
String fileName="D:"+File.separator+"hello.txt";
File f=new File(fileName);
char[] ch=new char[100];
Reader read=new FileReader(f);
int temp=0;
int count=0;
while((temp=read.read())!=(-1)){
ch[count++]=(char)temp;
}
read.close();
System.out.println("內容為"+new String(ch,0,count));
}
實際上位元組流在操作的時候本身是不會用到緩衝區的,是檔案本身的直接操作的,但是字元流在操作的 時候下後是會用到緩衝區的
,是通過緩衝區來操作檔案的。
讀者可以試著將上面的位元組流和字元流的程式的最後一行關閉檔案的代碼注釋掉,然後運行程式看看。你就會發現使用位元組流的話
,檔案中已經存在內容,但是使用字元流的時候,檔案中還是沒有內容的,這個時候就要重新整理緩衝區。
OutputStreramWriter將輸出的字元流轉化為位元組流
InputStreamReader將輸入的位元組流轉換為字元流
但是不管如何操作,最後都是以位元組的形式儲存在檔案中的。
public static void main(String[] args){
// 此刻直接輸出到螢幕
System.out.println("hello");
File file = new File("d:" + File.separator + "hello.txt");
try{
System.setOut(new PrintStream(new FileOutputStream(file)));
}catch(FileNotFoundException e){
e.printStackTrace();
}
System.out.println("這些內容在檔案中才能看到哦!");
}
}
【運行結果】:
eclipse的控制台輸出的是hello。然後當我們查看d盤下面的hello.txt檔案的時候,會在裡面看到:這些內容在檔案中才能看到哦
!
public static void main(String[] args){
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str = null;
System.out.println("請輸入內容");
try{
str = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你輸入的內容是:" + str);
}
BufferedReader只能接受字元流的緩衝區,因為每一個中文需要佔據兩個位元組,所以需要將System.in這個位元組輸入資料流變為字元輸入
流,
位元組流:一次讀入或讀出是8位二進位。
字元流:一次讀入或讀出是16位二進位。
位元組流和字元流的原理是相同的,只不過處理的單位不同而已。尾碼是Stream是位元組流,而尾碼是Reader,Writer是字元流。
使用物件流程需要實現Serializable介面,否則會報錯。
以stream都是位元組流,以reader、writer都是字元流,其中2個是充當的轉換器intputStreamReader和OntputStreamWriter,緩衝流
就是加上了buffered,為什麼要緩衝流呢:
把低級流用緩衝流封裝,可以加快讀寫的速度
最原始的位元組流沒有用到緩衝區,但是你可以給它套一個緩衝流吧
轉化流:InputStreamReader/OutputStreamWriter,把位元組轉化成字元
Buffered緩衝流::BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter,是帶緩衝區的處理流,緩衝
區的作用的主要目的是:避免每次和硬碟打交道,提高資料訪問的效率。
物件流程:ObjectInputStream,ObjectOutputStream,把封裝的對象直接輸出,而不是一個個在轉換成字串再輸出。
何時使用轉換流?
1.
如果使用非預設編碼儲存檔案或者讀取檔案時,需要用到轉換流,因為位元組流的重載構造方法中有指定編碼格式的參數,而FielReader 與 FileWriter 是預設編碼的文字檔
比如:
當我們使用預設GBK編碼儲存文本時,下面2句代碼其實是一樣的效果,
new OutputStreamWriter(new FileOutputStream("out.txt"))
new FileWriter("out.txt")
當要求儲存為其他編碼比如UTF-8時,就要這樣寫
new OutputStreamWriter(new FileOutputStream("out.txt"),"UTF-8")
而如果要讀取一個UTF-8編碼的文字檔時,同樣的要用
new InputStreamReader(new FileInputStream("in.txt"),"UTF-8");
而不能用new FileWriter("in.txt")
java IO流學習筆記