javaIO

1、分爲字節流和字符流;

    字符流中封裝的有編碼表,對字符的操作很方便;

    下面代碼爲字符流的應用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//將C盤一個文本文件複製到D盤。
/*
複製的原理:
其實就是將C盤下的文件數據存儲到D盤的一個文件中。
步驟:
1,在D盤創建一個文件。用於存儲C盤文件中的數據。
2,定義讀取流和C盤文件關聯。
3,通過不斷的讀寫完成數據存儲。
4,關閉資源。
*/
importjava.io.*;
classCopyText
{
publicstaticvoidmain(String[] args) throws IOException
{
copy_2();
}
publicstaticvoidcopy_2()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = newFileWriter("SystemDemo_copy.txt");
fr = newFileReader("SystemDemo.java");
char[] buf = newchar[1024];
intlen = 0;
while((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch(IOException e)
{
thrownewRuntimeException("讀寫失敗");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch(IOException e)
{
}
if(fw!=null)
try
{
fw.close();
}
catch(IOException e)
{
}
}
}
//從C盤讀一個字符,就往D盤寫一個字符。
publicstaticvoidcopy_1()throws IOException
{
//創建目的地。
FileWriter fw = newFileWriter("RuntimeDemo_copy.txt");
//與已有文件關聯。
FileReader fr = newFileReader("RuntimeDemo.java");
intch = 0;
while((ch=fr.read())!=-1)
{
fw.write(ch);
}
fw.close();
fr.close();
}
}



2、下面代碼爲字節流實例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
複製一個圖片
思路:
1,用字節讀取流對象和圖片關聯。
2,用字節寫入流對象創建一個圖片文件。用於存儲獲取到的圖片數據。
3,通過循環讀寫,完成數據的存儲。
4,關閉資源。
*/
importjava.io.*;
classCopyPic
{
publicstaticvoidmain(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = newFileOutputStream("c:\\2.bmp");
fis = newFileInputStream("c:\\1.bmp");
byte[] buf = newbyte[1024];
intlen = 0;
while((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch(IOException e)
{
thrownewRuntimeException("複製文件失敗");
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch(IOException e)
{
thrownewRuntimeException("讀取關閉失敗");
}
try
{
if(fos!=null)
fos.close();
}
catch(IOException e)
{
thrownewRuntimeException("寫入關閉失敗");
}
}
}
}



3、字節流和字符流之間的轉換

   字節流轉爲字符流:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
importjava.io.*;
classTransStreamDemo
{
publicstaticvoidmain(String[] args) throws IOException
{
//獲取鍵盤錄入對象。
//InputStream in = System.in;
//將字節流對象轉成字符流對象,使用轉換流。InputStreamReader
//InputStreamReader isr = new InputStreamReader(in);
//爲了提高效率,將字符串進行緩衝區技術高效操作。使用BufferedReader
//BufferedReader bufr = new BufferedReader(isr);
//鍵盤的最常見寫法。
BufferedReader bufr =
newBufferedReader(newInputStreamReader(System.in));
//      OutputStream out = System.out;
//      OutputStreamWriter osw = new OutputStreamWriter(out);
//      BufferedWriter bufw = new BufferedWriter(osw);
BufferedWriter bufw = newBufferedWriter(newOutputStreamWriter(System.out));
Stringline = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}

3、File類:

    由於文件時數據操作的基本單位,所以將File封裝爲一個類;

    File file=new File("sd.txt")即可;

    在流中,使用流的方法創建的文件其效果與這個一樣;

4、一種特殊文件,Properties文件是以鍵值對存儲數據的;

  它是HashTable的子類,具備Map的特點,是集合中和IO相結合的一個類;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
importjava.io.*;
importjava.util.*;
classPropertiesDemo
{
publicstaticvoidmain(String[] args) throws IOException
{
//method_1();
loadDemo();
}
publicstaticvoidloadDemo()throws IOException
{
Properties prop = newProperties();
FileInputStream fis = newFileInputStream("info.txt");
//將流中的數據加載進集合。
prop.load(fis);
prop.setProperty("wangwu","39");
FileOutputStream fos = newFileOutputStream("info.txt");
prop.store(fos,"haha");
//  System.out.println(prop);
prop.list(System.out);
fos.close();
fis.close();
}
//演示,如何將流中的數據存儲到集合中。
//想要將info.txt中鍵值數據存到集合中進行操作。
/*
1,用一個流和info.txt文件關聯。
2,讀取一行數據,將該行數據用"="進行切割。
3,等號左邊作爲鍵,右邊作爲值。存入到Properties集合中即可。
*/
publicstaticvoidmethod_1()throws IOException
{
BufferedReader bufr = newBufferedReader(newFileReader("info.txt"));
Stringline = null;
Properties prop = newProperties();
while((line=bufr.readLine())!=null)
{
String[] arr = line.split("=");
///System.out.println(arr[0]+"...."+arr[1]);
prop.setProperty(arr[0],arr[1]);
}
bufr.close();
System.out.println(prop);
}
//  設置和獲取元素。
publicstaticvoidsetAndGet()
{
Properties prop = newProperties();
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","39");
//      System.out.println(prop);
Stringvalue = prop.getProperty("lisi");
//System.out.println(value);
prop.setProperty("lisi",89+"");
Set<String> names = prop.stringPropertyNames();
for(Strings : names)
{
System.out.println(s+":"+prop.getProperty(s));
}
}
}


  5、其他常見IO流對象:ByteArrayStream、DataStream、RandomAccessFile、

         PipedStream、PrintWrite以及IO的合併流;

         其中RandomAccessFile直接繼承於Object;

本文出自 “爲夢而狂” 博客,請務必保留此出處http://xzb09.blog.51cto.com/7243998/1251248

1、分爲字節流和字符流;

    字符流中封裝的有編碼表,對字符的操作很方便;

    下面代碼爲字符流的應用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//將C盤一個文本文件複製到D盤。
/*
複製的原理:
其實就是將C盤下的文件數據存儲到D盤的一個文件中。
步驟:
1,在D盤創建一個文件。用於存儲C盤文件中的數據。
2,定義讀取流和C盤文件關聯。
3,通過不斷的讀寫完成數據存儲。
4,關閉資源。
*/
importjava.io.*;
classCopyText
{
publicstaticvoidmain(String[] args) throws IOException
{
copy_2();
}
publicstaticvoidcopy_2()
{
FileWriter fw = null;
FileReader fr = null;
try
{
fw = newFileWriter("SystemDemo_copy.txt");
fr = newFileReader("SystemDemo.java");
char[] buf = newchar[1024];
intlen = 0;
while((len=fr.read(buf))!=-1)
{
fw.write(buf,0,len);
}
}
catch(IOException e)
{
thrownewRuntimeException("讀寫失敗");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch(IOException e)
{
}
if(fw!=null)
try
{
fw.close();
}
catch(IOException e)
{
}
}
}
//從C盤讀一個字符,就往D盤寫一個字符。
publicstaticvoidcopy_1()throws IOException
{
//創建目的地。
FileWriter fw = newFileWriter("RuntimeDemo_copy.txt");
//與已有文件關聯。
FileReader fr = newFileReader("RuntimeDemo.java");
intch = 0;
while((ch=fr.read())!=-1)
{
fw.write(ch);
}
fw.close();
fr.close();
}
}



2、下面代碼爲字節流實例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
複製一個圖片
思路:
1,用字節讀取流對象和圖片關聯。
2,用字節寫入流對象創建一個圖片文件。用於存儲獲取到的圖片數據。
3,通過循環讀寫,完成數據的存儲。
4,關閉資源。
*/
importjava.io.*;
classCopyPic
{
publicstaticvoidmain(String[] args)
{
FileOutputStream fos = null;
FileInputStream fis = null;
try
{
fos = newFileOutputStream("c:\\2.bmp");
fis = newFileInputStream("c:\\1.bmp");
byte[] buf = newbyte[1024];
intlen = 0;
while((len=fis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
}
catch(IOException e)
{
thrownewRuntimeException("複製文件失敗");
}
finally
{
try
{
if(fis!=null)
fis.close();
}
catch(IOException e)
{
thrownewRuntimeException("讀取關閉失敗");
}
try
{
if(fos!=null)
fos.close();
}
catch(IOException e)
{
thrownewRuntimeException("寫入關閉失敗");
}
}
}
}



3、字節流和字符流之間的轉換

   字節流轉爲字符流:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
importjava.io.*;
classTransStreamDemo
{
publicstaticvoidmain(String[] args) throws IOException
{
//獲取鍵盤錄入對象。
//InputStream in = System.in;
//將字節流對象轉成字符流對象,使用轉換流。InputStreamReader
//InputStreamReader isr = new InputStreamReader(in);
//爲了提高效率,將字符串進行緩衝區技術高效操作。使用BufferedReader
//BufferedReader bufr = new BufferedReader(isr);
//鍵盤的最常見寫法。
BufferedReader bufr =
newBufferedReader(newInputStreamReader(System.in));
//      OutputStream out = System.out;
//      OutputStreamWriter osw = new OutputStreamWriter(out);
//      BufferedWriter bufw = new BufferedWriter(osw);
BufferedWriter bufw = newBufferedWriter(newOutputStreamWriter(System.out));
Stringline = null;
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}

3、File類:

    由於文件時數據操作的基本單位,所以將File封裝爲一個類;

    File file=new File("sd.txt")即可;

    在流中,使用流的方法創建的文件其效果與這個一樣;

4、一種特殊文件,Properties文件是以鍵值對存儲數據的;

  它是HashTable的子類,具備Map的特點,是集合中和IO相結合的一個類;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
importjava.io.*;
importjava.util.*;
classPropertiesDemo
{
publicstaticvoidmain(String[] args) throws IOException
{
//method_1();
loadDemo();
}
publicstaticvoidloadDemo()throws IOException
{
Properties prop = newProperties();
FileInputStream fis = newFileInputStream("info.txt");
//將流中的數據加載進集合。
prop.load(fis);
prop.setProperty("wangwu","39");
FileOutputStream fos = newFileOutputStream("info.txt");
prop.store(fos,"haha");
//  System.out.println(prop);
prop.list(System.out);
fos.close();
fis.close();
}
//演示,如何將流中的數據存儲到集合中。
//想要將info.txt中鍵值數據存到集合中進行操作。
/*
1,用一個流和info.txt文件關聯。
2,讀取一行數據,將該行數據用"="進行切割。
3,等號左邊作爲鍵,右邊作爲值。存入到Properties集合中即可。
*/
publicstaticvoidmethod_1()throws IOException
{
BufferedReader bufr = newBufferedReader(newFileReader("info.txt"));
Stringline = null;
Properties prop = newProperties();
while((line=bufr.readLine())!=null)
{
String[] arr = line.split("=");
///System.out.println(arr[0]+"...."+arr[1]);
prop.setProperty(arr[0],arr[1]);
}
bufr.close();
System.out.println(prop);
}
//  設置和獲取元素。
publicstaticvoidsetAndGet()
{
Properties prop = newProperties();
prop.setProperty("zhangsan","30");
prop.setProperty("lisi","39");
//      System.out.println(prop);
Stringvalue = prop.getProperty("lisi");
//System.out.println(value);
prop.setProperty("lisi",89+"");
Set<String> names = prop.stringPropertyNames();
for(Strings : names)
{
System.out.println(s+":"+prop.getProperty(s));
}
}
}


  5、其他常見IO流對象:ByteArrayStream、DataStream、RandomAccessFile、

         PipedStream、PrintWrite以及IO的合併流;

         其中RandomAccessFile直接繼承於Object;

本文出自 “爲夢而狂” 博客,請務必保留此出處http://xzb09.blog.51cto.com/7243998/1251248


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章