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


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