標籤:bsp rect while ack iter adl file rac public
1、java流分為兩種
(1)位元組流:可以用於讀寫二進位檔案及任何類型檔案byte
(2)字元流:可以用於讀寫文字檔,不能操作二進位檔案
位元組流 字元流
輸入 InputStream Reader
輸出 OutputStream Writer
2、File類的基本用法
import java.io.*;
public class Io{
File f1 = new File("e:\\aa.txt");//建立一個檔案對象
System.out.println("檔案路徑"+f1.getAbsolutePath());//得到檔案的路徑
System.out.println("檔案的大小"+f1.length());//得到檔案的大小,位元組數
File f3 = new File("e:\\ff");//建立檔案夾
if(f3.isDirectory()){//判斷檔案夾是否存在
System.out.println("檔案夾存在,不能建立!");
}else{
f3.mkdir();//建立檔案夾
}
File f2 = new File("e:\\ff\\hsp.txt");//建立檔案和建立檔案夾
if(!f2.exists()){//判斷檔案是否存在
try{//可以存在
f2.createNewfile();//建立一個新檔案
}catch(Exception e){
e.printStackTrace();
}
}else{
System.out.println("檔案存在,不能建立!");
}
File f4 = new File("e:\\ff");\\列出一個檔案夾下面的所有檔案
if(f4.isDirectory()){\\判斷檔案夾是否存在
File lists[] = f4.listFiles();//將檔案夾的檔案,傳給lists數組
for(int i=0;i<lists.length;i++){//遍曆數組
System.out.println("顯示出檔案名稱是"+lists[i].getName());//輸出檔案夾下所有的檔案名稱
}
}
}
3.讀取檔案
import java.io.*;
public class Io{
public static void main(String[] args){
File f = new File("e:\\ff\\hsp.txt");//得到一個檔案對象,f指向e:ff\hsp.txt檔案
FileInputStream fis = null;
try{
fis = new FileInputStream(f);//定義一個位元組數組,相當於緩衝
byte []bytes = new byte[1024];
int n=0;//得到實際讀取到的位元組數
while((n=fis.read(bytes))!=-1){//迴圈讀取
String s = new String(bytes,0,n);//把位元組轉成String
System.out.println(s);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{//關閉檔案流必須放在finally語句塊中
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
4、從鍵盤接收使用者輸入內容,並儲存到檔案中
import java.io.*;
public class Io{
public static void main(String[] args){
File f = new File("e:\\ff\\ss.txt");//直接覆蓋寫同一個檔案
FileOutputStream fos = null;
if(f.exists()){
System.out.println("檔案已存在");
}else{
try{
fos = new FileOutputStream(f);
String s ="hello,world!\r\n";
String s1="中國人";
fos.write(s.getBytes());
fos.write(s1.getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fos.close();
}catch(Exception e2){
e2.printStackTrace();
}
}
}
}
}
5、圖片拷貝
import java.io.*;
public class Io{
public static void main(String[] args){
FileInputStream fis = null;//輸入資料流先將圖片讀到記憶體 再讀到檔案
FileOutputStream fos = null;//輸出資料流
try{
fis= new FileInputStream("e:\\ff\\a.jsp");
fos = new FileOutputStream("e:\\a.jsp");
byte buf[] = new byte[1024];
int n=0;//記錄實際讀取到的位元組數
while ((n=fis.read(buf))!=-1){//迴圈讀取圖片
fos.write(buf);//輸出到指定檔案
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fis.close();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
6.讀取一個檔案並寫入到另一個檔案中char[]來中轉
import java.io.*;
public class Io{
public static void main(String[] args){
FileReader fr = null;//檔案取出字元流對象(輸入資料流)
FileWriter fw = null;
try{
fr= new FileReader("e:\\ff\\hsp.txt");//建立fr對象
fw = new FileWriter("e:\\hsp.txt");//建立輸出對象
char c[] = new char[1024];
int n=0;//讀入到記憶體
while ((n=fr.read(c))!=-1){
String s = new String(c,0,n);
System.out.println(s);
fw.write(c,0,n);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fr.close();
fw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
7、緩衝字元流
import java.io.*;
public class Io{
public static void main(String[] args){
BufferedReader br = null;
BufferedWriter bw = null;
try{
FileReader fr = new FileReader("e:\\ff\\hsp.txt");
br = new BufferedReader(fr);
FileWriter bw = new FileWriter("e:\\hsp.txt");
bw = new BufferedWriter(fw);
String s= "";//迴圈讀取
while ((s=br.readLine())!=null){
bw.write(s+"\r\n");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
br.close();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
韓順平 java筆記 第43-46講 io編程