IO

来源:互联网 发布:淘宝客内部优惠券 编辑:程序博客网 时间:2024/06/07 10:47
字符流输入输出
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
//将C盘一个文本文件复制到D盘。
 
/*
复制的原理:
其实就是将C盘下的文件数据存储到D盘的一个文件中。
 
步骤:
1,在D盘创建一个文件。用于存储C盘文件中的数据。
2,定义读取流和C盘文件关联。
3,通过不断的读写完成数据存储。
4,关闭资源。
*/
 
import java.io.*;
 
class CopyText 
{
    public static void main(String[] args) throws IOException
    {
        copy_2();
    }
 
 
    public static void copy_2()
    {
        FileWriter fw = null;
        FileReader fr = null;
        try
        {
            fw = new FileWriter("SystemDemo_copy.txt");
            fr = new FileReader("SystemDemo.java");
 
            char[] buf = new char[1024];
 
            int len = 0;
            while((len=fr.read(buf))!=-1)
            {
                fw.write(buf,0,len);
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException("读写失败");
 
        }
        finally
        {
            if(fr!=null)
                try
                {
                    fr.close();//有可能找不到fr但是要执行close所以要再处理异常
                }
                catch (IOException e)
                {
                }
            if(fw!=null)
                try
                {
                    fw.close();
                }
                catch (IOException e)
                {
                }
        }
    }
Buffer缓冲区  
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
/*
缓冲区的出现是为了提高流的操作效率而出现的。
 
所以在创建缓冲区之前,必须要先有流对象。
 
该缓冲区中提供了一个跨平台的换行符。
newLine();
 buffer 装饰类
*/
 
import java.io.*;
 
class  BufferedWriterDemo
{
    public static void main(String[] args) throws IOException
    {
       
           bufr = new BufferedReader(new FileReader("BufferedWriterDemo.java"));
            bufw = new BufferedWriter(new FileWriter("bufWriter_Copy.txt"));
 
            String line = null;
 
            while((line=bufr.readLine())!=null)     注意readline读出的是String  read读出的是字节   如果想要获得字节对应的数据类型则前                                                       面转换加(类型)比如(char)*system.in.read())

            {
                bufw.write(line);
                bufw.newLine();
                bufw.flush();
 
            }
 
    }
}

StringreadLine()
读取一个文本行。

voidnewLine()
写入一个行分隔符。



字符转字节  字节转字符
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
class  TransStreamDemo2
{
    public static void main(String[] args) throws IOException
    {
        System.setIn(new FileInputStream("PersonDemo.java"));
 
        System.setOut(new PrintStream("zzz.txt"));
 
        //键盘的最常见写法。
        BufferedReader bufr = 
                new BufferedReader(new InputStreamReader(System.in));
 
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d.txt"),"UTF-8");//可加入编码表
 
        String line = null;
 
        while((line=bufr.readLine())!=null)  也可以比如输入的是字符(char)(system.in.read())
        {
            if("over".equals(line))
                break;
            bufw.write(line.toUpperCase());
            bufw.newLine();
            bufw.flush();
        }
 
        bufr.close();
 
    }
}
FileReader  FileWriter 即是new InputStreamReader(new FileOutputStream("d.txt"),GBK)默认码表GBK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 //键盘的最常见写法。
            BufferedReader bufr = 
                    new BufferedReader(new InputStreamReader(System.in));
      
            BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//可加入编码表
      
            String line = null;
      
            while((line=bufr.readLine())!=null)  
            {
                if("over".equals(line))
                    break;
                bufw.write(line.toUpperCase());
                bufw.newLine();
                bufw.flush();
            }
      
            bufr.close();

Serializable对象

最重要的两个原因是:

  1、将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本;
  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
实现:要序列化一个对象,先要创建某些OutputStream对象,然后将其封装在一个ObjectOutputStream对象内,再调用writeObject()方法即可序列化一个对象;反序列化也类似。 
实现serializable 接口也就是给对象赋予一个uuid在辨认对象时好区分
 ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

注意是把堆中的变量序列化 如果是静态或者是transiant修饰则不能序列化
import java.io.*; 
 
public class Person implements Serializable { 
private String userName; 
private String password; 
 
public Person(String userName, String password) { 
this.userName = userName; 
this.password = password; 
 
public String toString() { 
return "userName:" + userName + "  password:" + password; 
 
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { 
//序列化一个对象(存储到一个文件) 
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.out")); 

oos.writeObject(new Person("Bruce""123456")); 
oos.close(); 
 
//反序列化,将该对象恢复(存储到一个文件) 
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.out")); 

Person p = (Person)ois.readObject(); 

RandomAccessFile
随机读取文件,底层是一个数组可以实现文件的分段读写
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
/*
RandomAccessFile
 
该类不是算是IO体系中子类。
而是直接继承自Object。
 
但是它是IO包中成员。因为它具备读和写功能。
内部封装了一个数组,而且通过指针对数组的元素进行操作。
可以通过getFilePointer获取指针位置,
同时可以通过seek改变指针的位置。
 
 
其实完成读写的原理就是内部封装了字节输入流和输出流。
 
通过构造函数可以看出,该类只能操作文件。
而且操作文件还有模式:只读r,,读写rw等。
 
如果模式为只读 r。不会创建文件。会去读取一个已存在文件,如果该文件不存在,则会出现异常。
如果模式rw。操作的文件不存在,会自动创建。如果存则不会覆盖。
 
*/
class RandomAccessFileDemo 
{
    public static void main(String[] args) throws IOException
    {
        //writeFile_2();
        //readFile();
 
        //System.out.println(Integer.toBinaryString(258));
 
    }
 
    public static void readFile()throws IOException
    {
        RandomAccessFile raf = new RandomAccessFile("ran.txt","r");
         
        //调整对象中指针。
        //raf.seek(8*1);
 
        //跳过指定的字节数
        raf.skipBytes(8);
 
        byte[] buf = new byte[4];
 
        raf.read(buf);
 
        String name = new String(buf);
 
        int age = raf.readInt();
 
 
        System.out.println("name="+name);
        System.out.println("age="+age);
 
        raf.close();
 
 
    }
Scanner
输入直接输出各种date数据
1
2
3
4
5
6
7
8
9
10
11
public class TestScanner { 
        public static void main(String[] args) { 
                Scanner s = new Scanner(System.in); 
                System.out.println("请输入字符串:"); 
                while (true) { 
                        String line = s.nextLine(); 
                        //int temp = in.nextInt();
                        if (line.equals("exit")) break
                        System.out.println(">>>" + line); 
                
        
}
PrintWriter 
类似于buffer但可以加入格式
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
/*
打印流:
该流提供了打印方法,可以将各种数据类型的数据都原样打印。
 
字节打印流:
PrintStream
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
 
 
 
字符打印流:
PrintWriter
构造函数可以接收的参数类型:
1,file对象。File
2,字符串路径。String
3,字节输出流。OutputStream
4,字符输出流,Writer。
 
 
*/
 
import java.io.*;
 
class  PrintStreamDemo
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader bufr = 
            new BufferedReader(new InputStreamReader(System.in));
 
        PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);   可以就收字节流也可以接受字符流
 
        String line = null;
 
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            out.println(line.toUpperCase());
            //out.flush();
        }
 
        out.close();
        bufr.close();
 
    }  
}

在线工具 由 开源中国 所有 | @新浪微博 |阿里云提供服务器和带宽 |意见反馈 | 


 
0 0
原创粉丝点击