java I/O 以及文件编码
參考:http://www.blogjava.net/haizhige/archive/2008/08/03/219668.html
總結一下就是:
*stream 是對字節進行操作
*Reader, *Writer 是對字符進行操作,既然是對字符操作,肯定會涉及到解碼加碼的操作。
import java.util.*; import java.io.*;public class testJava {/*** @param args*/ public static void main(String[] args) {try {BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("tt"),"utf-8"));//用BufferedReader加速讀取String line = in.readLine();byte [] bytes = line.getBytes("utf-8");//打印line的utf-8編碼for (byte b : bytes)System.out.print(b+" ");System.out.println();for (byte b: bytes)System.out.print(Integer.toHexString(b & 0xff) + " ");System.out.println();//打印line的內部編碼,注意,內部已經是unicode編碼for (int i = 0; i < line.length(); ++i) {System.out.print(Integer.toHexString((int)line.charAt(i)) + " ");}System.out.println();in.close();//直接按照字節讀取文件FileInputStream fin = new FileInputStream("tt");int len = fin.available();byte [] data = new byte[len];fin.read(data);for (byte b : data)System.out.print(Integer.toHexString(b & 0xff) + " ");fin.close();}catch (IOException e) {e.printStackTrace();}}}tt文件內容是
我是abc
utf8編碼,dos格式,bom頭
輸出是:
-17 -69 -65 -26 -120 -111 -26 -104 -81 97 98 99?
ef bb bf e6 88 91 e6 98 af 61 62 63?
feff 6211 662f 61 62 63?
ef bb bf e6 88 91 e6 98 af 61 62 63 d a?
如果這個java文件是utf-8編碼的,則
String s = "我是abc";
for (int i = 0; i < s.length(); ++i)
System.out.print(Integer.toHexString((int)s.charAt(i))+" ");
System.out.println();
s就已經是unicode編碼的了
Integer.toHexString(b & 0xff)
b是一個byte, 如果直接Integer.toHexString(b), 因為參數是int類型的,所以第一步是將b變為int,如果b在0-127之間的,那是沒問題,如果b在128-255的,首先檢測到符號位是1,就把前面的字節都補全為1,比如b=128?
二進制是 1000 0000
變成整形的二進制是 1111 1111 1000 0000
b & 0xff ,就是b & 0x00ff
1、描述:流是字節數據或字符數據序列。Java采用輸入流對象和輸出流對象來支持程序對數據的輸入和輸出。輸入流對象提供了數據從源點流向程序的管道,程序可以從輸入流對象讀取數據;輸出流對象提供了數據從程序流向終點的管道,程序通過該管道把數據寫到終點。所有的關于輸入/輸出的類都包含在java.io的包中。
2、File類:它主要關心的是文件的具體屬性,而非內容,定義了許多方法,實現對文件的創建、刪除等操作。
code:
import java.io.*;
public class Test
{
?public static void main(String args[])throws Exception
?{
??File file1=new File("w1.txt");//在當前目錄下
??file1.createNewFile();//得到文件w1.txt
??file1.mkdir();//得到目錄w1.txt
??File file2=new File("D:\\javaprogram\\text\\w2.txt");//指定目錄
??file2.createNewFile();//得到文件w2.txt
??//用靜態字段separator獲得系統分隔符,保證程序通用性
??File fDir=new File(File.separator);//作為字符是'\',作為File對象為當前根目錄
??String fStr="javaprogram"+File.separator+"text"+File.separator+"w3.txt";
??File file3=new File(fDir,fStr);
??file3.createNewFile();//得到文件w3.txt
??file1.delete();
??file2.delete();
??file3.delete();
??//delete()方法刪除文件,調用即刪除,而deleteOnExit()是在JVM終止時才刪除
??//這樣就得以建立臨時文件以存儲臨時數據,臨時文件保存在臨時文件夾
??//要找臨時文件夾,請查看環境變量temp的設置
??for(int i=0;i<5;i++)
??{
???File file=File.createTempFile("wang",".temp");
???file.deleteOnExit();
??}
??Thread.sleep(3000);
??//下面的一段程序將實現,打印指定目錄下的.java文件的信息
??File f=new File("d:\\javaprogram");
??if(f.exists())//判斷文件是否存在
??{
???if(f.isDirectory())//判斷文件是目錄還是標準文件
???{
????//調用帶參數的listFiles()方法返回滿足特定過慮器的
????//此抽象路徑名所表示目錄中的文件和目錄的抽象路徑名數組
????File[] fname=f.listFiles(new FilenameFilter()
????{
?????//匿名類實現接口FilenameFileter的唯一方法
?????public boolean accept(File dir, String name)?
?????{
??????return name.indexOf(".java")!=-1;
?????}
????});
????for(int i=0;i<fname.length;i++)
????{
?????System.out.println(fname[i]);
????}
???}
??}
??else
??{
???System.out.println("文件夾不存在.");
??}
?}
}
3、字節流:
程序運行中常的I/O操作包括向標準設備輸入輸出數據和文件輸入輸出。對于前者,java定義了三個直接使用的流對象:System.err(標準錯誤輸出)、System.out(標準輸出)和System.in(標準輸入);對于后者,可以使用FileOutputStream和FileInputStream。
code:
import java.io.*;
public class Test
{
?static final String file1="D:\\javaprogram\\w1.txt";
?static final String file2="E:\\database\\w2.txt";
?static final String file3="E:\\wmpub\\w3.txt";
?public static void main(String args[])throws IOException
?{
??/*//關于System.in
??int data;
??while ((data=System.in.read())!=-1)//Ctrl+c結束輸入
??{
???System.out.write(data);
??}*/
??//下面的程序段用以向file1文件寫入,把其內容的一部分復制到file2
??//再把file2文件完整地復制到file3,并打印出來
??FileOutputStream fos1=new FileOutputStream(file1);
??FileOutputStream fos2=new FileOutputStream(file2);
??FileOutputStream fos3=new FileOutputStream(file3);
??fos1.write("今天是2008年8月3號,離北京奧運會還有5天,心里非常激動啊.".getBytes());
??fos1.close();
??FileInputStream fis1=new FileInputStream(file1);
??fis1.skip(19);//跳過19個字節
??byte[] buf=new byte[fis1.available()];
??fis1.read(buf);
??fis1.close();
??System.out.println(new String(buf));
??fos2.write(buf);
??fos2.close();
??FileInputStream fis2=new FileInputStream(file2);
??while(fis2.available()>0)
??{
???byte[] b=new byte[fis2.available()];
???int let=fis2.read(b);
???if(let==-1)break;
???fos3.write(b,0,let);
??}
??System.out.println("復制成功!");
??fis2.close();
??fos3.close();
??//以下程序段實現了一個圖像文件的復制
??FileInputStream a=new FileInputStream("4.jpg");
??FileOutputStream b=new FileOutputStream("3.jpg");
??byte c[]=new byte[a.available()];
??a.read(c);
??b.write(c);
??a.close();
??b.close();
?}
}
4、字符流(FileWriter and FileReader)
import java.io.*;
public class Test
{
?public static void main(String args[])
?{
??//本程序完成從控制臺讀入文件名,并實現復制
??int length;
??char buf[]=new char[100];
??try
??{
???FileReader in=new FileReader(args[0]);
???FileWriter out=new FileWriter(args[1]);
???length=in.read(buf);
???while(length!=-1)
???{
????out.write(buf,0,length);
????//字符流讀取通過read()的返回值判斷是否讀到文件末尾
????//我剛才忘記加這條語句,文件被一直寫,都死機了,重啟后一看,寫了2.6G
????length=in.read(buf);
???}
???in.close();
???out.close();
??}
??catch (IOException e)
??{
???e.printStackTrace();
??}
?}
}?
5、過濾流之一
????Java利用過濾流可以在讀寫數據的同時對數據進行處理,以達到性能的改善,提高程序執行效率。將過濾流和某個輸入流或輸出流(節點流)連接。連接是通過在過濾流的構造方法中指定入口參數——節點流來實現的。
§BufferedInputStream:
????????????????????????FileInputStream fis=new FileInputStream("w1.txt");
????????????????????????BufferedInputStream bis=new BufferedInputStream(fis);
??????????????????????? byte[] buf=new byte[100];
????????????????????????int len=bis.read(buf,0,len);
????????????????????????System.out.println(new String(buf,0,len));
????????????????????????bis.close();
????????§BufferedOutputStream:
????????????????????????FileOutStream fos=new FileOutputStream("w2.txt");
????????????????????????BufferedOutputStream bos=new BufferedOutputStream(bos);
????????????????????????bos.write("好好學習,天天向上".getBytes());
????????????????????????bos.flush();
????????????????????????bos.close();
????也可以是匿名創建:如:BufferedOutputStream?bos=new BufferedOutputStream(new FileInputStream("w2.txt"));?
?????BufferedReader和BufferedWirter與此類似,不過是分別指定Writer和Reader類型的參數罷了。
一個實例程序:
import java.io.*;
public class Test
{
?public static void main(String[] args)
?{
??try
??{
???FileInputStream in=new FileInputStream(args[0]);
???BufferedInputStream bufIn=new BufferedInputStream(in);
???int limit;
???bufIn.mark(limit=bufIn.available());//在當前位置打上標記
???for(int i=0;i<limit;i++)
????System.out.print((char)(bufIn.read()));
???System.out.println();
???bufIn.reset();//reset緩沖區標志
???int c;
???while((c=bufIn.read())>=0)System.out.print((char)c);
???bufIn.close();
??}
??catch (IOException e)
??{
???e.printStackTrace();
??}
?}
}
import java.io.*;
public class Test
{
?public static void main(String args[])
?{
??try
??{
???DataOutputStream dos=new DataOutputStream(
????new BufferedOutputStream(new FileOutputStream(
?????"d://javaprogram//w.txt")));
???dos.writeInt(5);
???dos.writeUTF("你好");
???dos.writeBoolean(true);
???dos.writeDouble(3.1415926);
???dos.writeBytes("ok!");
???dos.writeChars("bye bye");
???dos.close();
???DataInputStream dis=new DataInputStream(
????new BufferedInputStream(new FileInputStream(
??????? "d://javaprogram//w.txt")));
???//讀出的順序應與寫入的順序一致
???System.out.println(dis.readInt());
???System.out.println(dis.readUTF());
???System.out.println(dis.readBoolean());
???System.out.println(dis.readDouble());
???byte b[]=new byte[3];
???dis.readFully(b);
???for(int j=0;j<3;j++)System.out.print((char)b[j]);
???System.out.println();
???StringBuffer st3=new StringBuffer();
???for(int j=0;j<7;j++)st3.append(dis.readChar());
???System.out.println(st3.toString());
???dis.close();
??}
??catch (IOException e)
??{
???System.err.println(e.toString());
??}
?}
}
6、過濾流之三:I/O流的鏈接圖:
? ?
7、字節和Unicode字符的橋梁:InputStreamReader、OutputStreamWriter
code:
import java.io.*;
public class Test
{
?public static void main(String[] args)throws IOException
?{
??//文件讀寫
??FileOutputStream fos=new FileOutputStream("w.txt");
??OutputStreamWriter osw=new OutputStreamWriter(fos);
??BufferedWriter bw=new BufferedWriter(osw);
??bw.write("今天是2008年8月3日,離北京奧運還有5天!");
??bw.close();
??FileInputStream fis=new FileInputStream("w.txt");
??InputStreamReader isr=new InputStreamReader(fis);
??BufferedReader br=new BufferedReader(isr);
??System.out.println(br.readLine());
??br.close();
??//控制臺讀寫
??BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
??BufferedWriter bw1=new BufferedWriter(new OutputStreamWriter(System.out));
??String strLine,str="";
??while((strLine=br1.readLine())!=null)//Ctrl+c結束輸入
??{
???str+=strLine;
???System.out.println(strLine);
??}
??br1.close();
??bw1.write(str,0,str.length());
??bw1.write((int)('\n')); //將回車符輸入bw1
??bw1.flush();
??bw1.close();
?}
}
8、管道流:PipedInputStream、PipedOutputStream
??????作用:用于線程間的通信,一個線程的pipedInputStream對象從另一個線程的PipedOutputStream對象讀取輸入,要使管道流有用,必須同時構造管道輸入流和管道輸出流。
例子(孫鑫老師的例子):
import java.io.*;
class Producer extends Thread
{
?private PipedOutputStream pos;
?public Producer(PipedOutputStream pos)
?{
??this.pos=pos;
?}
?public void run()
?{
??try
??{
???pos.write("Hello,welcome you!".getBytes());
??}
??catch (Exception e)
??{
???e.printStackTrace();
??}
?}
}
class Consumer extends Thread
{
?private PipedInputStream pis;
?public Consumer(PipedInputStream pis)
?{
??this.pis=pis;
?}
?public void run()
?{
??try
??{
???byte[] buf=new byte[100];
???int len=pis.read(buf);
???System.out.println(new String(buf,0,len));
???pis.close();
??}
??catch (Exception e)
??{
???e.printStackTrace();
??}
?}
}
public class Test
{
?public static void main(String args[])
?{
??PipedOutputStream pos=new PipedOutputStream();
??PipedInputStream pis=new PipedInputStream();
??try
??{
???pos.connect(pis);
???new Producer(pos).start();
???new Consumer(pis).start();
??}
??catch (Exception e)
??{
???e.printStackTrace();
??}
?}
}
9、PrintWriter類:創建的輸出流可以使用print和println方法,按Unicode字符形式輸出,輸出的數據可讀性較好。
????????§打印流建立文本文件:
????????????????????????????????PrintWriter out=new PrintWriter(new FileWriter(1.dat));
????????????????????????????????String str="天呢,我告訴你吧:";
????????????????????????????????char[] z={'北','京','奧','運','會','在'};double g=2008;
????????????????????????????????out.println(st);out.print(z);out.print(g);out.println("年08月08日08時08分08秒");
????????????????????????????????out.close();
????????§打印流在屏幕上顯示文本
????????????????????????????????PrintWriter out=new PrintWriter(System.out);
????????????????????????????????其余同上,此處略
10、文件的隨機讀寫:RandomAccessFile
import java.io.*;
public class Test
{
?public static void main(String args[])throws Exception
?{
??int lineNo;//讀到的行號
??long fp;//文件指針
??BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
??RandomAccessFile raf=new RandomAccessFile("w.txt","rw");
??System.out.println("請輸入6個字符串");
??int[] len=new int[12];
??String[] str=new String[12];
??for(int i=0;i<6;i++)
??{
???System.out.println("行號"+(i+1)+":");
???str[i]=br.readLine();
???len[i]=str[i].length();
???raf.write((str[i]+"\n").getBytes());
??}
??while(true)
??{
???fp=0;
???raf.seek(0);
???System.out.println("你要顯示第幾行?"+"(1--6)");
???lineNo=Integer.parseInt(br.readLine());
???for(int i=1;i<lineNo;i++)
????fp=fp+(long)len[i-1]+1;
???raf.seek(fp);
???System.out.println("第"+lineNo+"行"+":"+raf.readLine());
???System.out.println("繼續嗎?"+"(y/n)");
???if((br.readLine().equals("n")))break;
??}
??raf.seek(raf.length());
??System.out.println("繼續寫入6行數據:");
??for(int i=6;i<12;i++)
??{
???System.out.println("行號"+(i+1)+":");
???str[i]=br.readLine();
???len[i]=str[i].length();
???raf.write((str[i]+"\n").getBytes());
??}
??System.out.println("打印12行數據:");
??raf.seek(0);
??for(long i=0;i<raf.length();i=raf.getFilePointer())
??{
???System.out.println(raf.readLine());
??}
??raf.close();
?}
}
11、文件的壓縮處理
?實例:
import java.io.*;
import java.util.*;
//ZipInputStream和ZipOutputStream在包java.util.zip中
import java.util.zip.*;
public class Test
{
?public static void main(String args[])throws Exception
?{
??//輸入若干文件名,將所有文件壓縮為w.zip
??ZipOutputStream zos=new ZipOutputStream(
???new BufferedOutputStream(new FileOutputStream("w.zip")));
??for(int i=0;i<args.length;i++)
??{
???BufferedInputStream bis=new BufferedInputStream(
????new FileInputStream(args[i]));
???//將每個要壓縮的文件稱為一個壓縮入口,使用ZipEntry生成壓縮入口對象
???//使用putNextEntry(ZipEntry entry)將壓縮入口加入到壓縮文件
???zos.putNextEntry(new ZipEntry(args[i]));
???int b;
???while((b=bis.read())!=-1)
????zos.write(b);
???bis.close();
??}
??zos.close();
??//解壓縮文件并顯示
??ZipInputStream zis=new ZipInputStream(
???new BufferedInputStream(new FileInputStream("w.zip")));
??ZipEntry z;
??while((z=zis.getNextEntry())!=null)//獲得入口
??{
???System.out.println(z.getName());//顯示文件初始名
???int x;
???while((x=zis.read())!=-1)
????System.out.write(x);
???System.out.println();
??}
??zis.close();
?}
}
12、編碼與解碼
?import java.util.*;
import java.nio.charset.*;
public class Test
{
?public static void main(String args[])throws Exception
?{
??//以下程序段打印當前計算機所能處理的標準 charset
??//Charset類位于java.nio.charset包中,此類定義了用于創建解碼器和編碼器
??//以及檢索與 charset 關聯的各種名稱的方法。此類的實例是不可變的。
??//Charset.availableCharsets()返回一個映射
??//Map是java.util包中的一個接口
??Map m=Charset.availableCharsets();
??//keySet()方法返回此映射中包含的鍵的 set 視圖
??Set names=m.keySet();
??//構造迭代器訪問諸元素
??Iterator it=names.iterator();
??while(it.hasNext())
??{
???System.out.println(it.next());
??}
??//Properties 類位于java.util包中,表示了一個持久的屬性集
??//System.getProperties()確定當前的系統屬性。
??Properties pps=System.getProperties();
??pps.list(System.out);//打印屬性
??//將系統文件的標準字符集改為:ISO-8859-1
??//設置的字符集,只是當前JVM上的字符集
??pps.put("file.encoding","ISO-8859-1");
??int data;
??byte[] buf=new byte[100];
??int i=0;
??while((data=System.in.read())!='q')
??{
???buf[i]=(byte)data;
???i++;
??}
??String str=new String(buf,0,1);
??System.out.println(str);
??//ISO-8859-1字符解碼為Unicode字符(java使用)
??//Unicode字符編碼為GBK字符
??//但并不是所有字符都能反編碼回來,比如漢字將丟失高字節
??String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
??System.out.println(strGBK);
?}
}?
13、對象序列化
???????????????對象的壽命常隨著對象的程序的終止而終止,倘若需要對對象的狀態進行保存,需要時再恢復。我們把對象的這種能夠記錄自己狀態以便將來再生的能力,叫做對象的持續性(persistence)。對象通過寫出描述自己狀態的值——對象轉化為字節流,來記錄自己的這個過程叫對象的序列化(serialization)。??一個對象要能夠實現序列化,必須實現Serializable接口,或者Externalizable接口。?
?????????????? 一個對象被序列化時,只保存對象的非靜態成員變量,如果一個對象的成員變量是一個對象,那么這個對象的數據成員也會被保存。如果一個可序列化的對象包含對某個不可序列化的對象的引用,那么整個序列化操作將會失敗,并且會拋出一個NotSerializableException。但如果把這個引用標記為transient,那么對象仍然可以序列化。
????????????????另外,對象序列化建立了一張對象網,將當前要序列化的對象中所持有的引用指向的對象都包含起來一起寫入到文件,如果一次序列化幾個對象,它們中的相同內容會被共享。
code:
import java.io.*;
public class Test
{
?public static void main(String[] args) throws Exception
?{
??Employee e1=new Employee("zhangsan",25,3000.50);
??Employee e2=new Employee("lisi",24,3200.40);
??Employee e3=new Employee("wangwu",27,3800.55);
??ObjectOutputStream oos=new ObjectOutputStream(
???new FileOutputStream("w.dat"));
??oos.writeObject(e1);
??oos.writeObject(e2);
??oos.writeObject(e3);
??oos.close();
??ObjectInputStream ois=new ObjectInputStream(
???new FileInputStream("w.dat"));
??Employee e;
??String strSal;
??for(int i=0;i<3;i++)
??{
???e=(Employee)ois.readObject();
???//設置自已需要的輸出方式
???//strSal=(e.salary==0)?"不告訴你":String.valueOf(e.salary);
???//System.out.println(e.name+":"+e.age+":"+strSal);
???System.out.println(e.name+":"+e.age+":"+e.salary);
??}
??ois.close();
?}
}
class Employee implements Serializable
{
?String name;
?int age;
?transient double salary;
?transient Thread t=new Thread();
?public Employee(String name,int age, double salary)
?{
??this.name=name;
??this.age=age;
??this.salary=salary;
?}
?//重寫方法,完成需要的操作,如果不重寫,要想不寫入某些數據可以標記為transient
?//本程序的數據salary就被標記為transient,打印時輸出為0.0,如果是String將為null
?/*private void writeObject(java.io.ObjectOutputStream oos)throws IOException
?{
??oos.writeUTF(name);
??oos.writeInt(age);
??//System.out.println("Write Object");
?}
?private void readObject(java.io.ObjectInputStream ois)throws IOException
?{
??name=ois.readUTF();
??age=ois.readInt();
??//System.out.println("Read Object");
}
Java Unsigned數據類型解決方案
在Java中,不存在Unsigned無符號數據類型,但可以輕而易舉的完成Unsigned轉換。
方案一:如果在Java中進行流(Stream)數據處理,可以用DataInputStream類對Stream中的數據以Unsigned讀取。
??????? Java在這方面提供了支持,可以用java.io.DataInputStream類對象來完成對流內數據的Unsigned讀取,該類提供了如下方法:
???????? (1)int?? readUnsignedByte()??? //從流中讀取一個0~255(0xFF)的單字節數據,并以int數據類型的數據返回。返回的數據相當于C/C++語言中所謂的“BYTE”。
????????? (2)int readUnsignedShort()?? //從流中讀取一個0~65535(0xFFFF)的雙字節數據,并以int數據類型的數據返回。返回的數據相當于C/C++語言中所謂的“WORD”,并且是以“低地址低字節”的方式返回的,所以程序員不需要額外的轉換。
方案二:利用Java位運算符,完成Unsigned轉換。
?????? 正常情況下,Java提供的數據類型是有符號signed類型的,可以通過位運算的方式得到它們相對應的無符號值,參見幾個方法中的代碼:
主要思想就是用更寬的類型表示窄類型的無符號數值
byte b = (byte)254;
System.out.println(b);//-2
int i = b&0x0ff;
System.out.println(i);//254
在轉換的時候要注意,原來的類型時n個字節,就用2*n個f做與運算,并且要在最前面加上0,將窄類型擴展的符號位清零
????? public int getUnsignedByte (byte data){????? //將data字節型數據轉換為0~255 (0xFF 即BYTE)。
???????? return data&0x0FF;
????? }
????? public int getUnsignedByte (short data){????? //將data字節型數據轉換為0~65535 (0xFFFF 即 WORD)。
??????????? return data&0x0FFFF;
????? }???????
???? public long getUnsignedIntt (int data){???? //將int數據轉換為0~4294967295 (0xFFFFFFFF即DWORD)。
???????? return data&0x0FFFFFFFFl;//在java中,默認字面整數數值都是int類型的,所以要在最后添加l表示是long
????? }
??????? 靈活的運用這些技法,根本不存“二進制在Java中得不到全面支持”的論斷!
總結
以上是生活随笔為你收集整理的java I/O 以及文件编码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Seafile - 最好的 Dropbo
- 下一篇: byte