• 尚学堂--IO


    知识点:http://www.cnblogs.com/liangyihui/p/5884615.html

    在DOS窗口输出程序本身:

    import java.io.*;
    
    public class Test {
        public static void main(String[] args) {
            int num = 0;//局部变量
            int b;      // 使用变量b来装调用read()方法时返回的整数
            FileInputStream in = null;//局部变量
    
            try {
                /*
                    使用FileInputStream流来读取有中文的内容时,读出来的是乱码,
                因为使用InputStream流里面的read()方法读取内容时是一个字节一个字节地读取的,
                而一个汉字是占用两个字节的,所以读取出来的汉字无法正确显示
                */
                in = new FileInputStream("E:\Test.java");
            } catch(FileNotFoundException e) {
                System.out.println("文件不存在");
            }
    
            try {
                while ((b = in.read()) != -1) {  // 调用int read() throws Exception方法时,返回的是一个int类型的整数
                    System.out.print((char)b);   //不加强制转换符输出的都是数字
                    num ++;  //若改为num = num ++;则输出:共输出了0个字节???????????????
                }    
    
                in.close();// 关闭输入流
                System.out.println();
                System.out.println("共输出了" + num + "个字节");
            } catch(IOException e) {
                e.printStackTrace();
            }    
        }//结束main
    }

     拷贝一个文档内容到另一个文档:

    import java.io.*;
    
    public class Test {
        public static void main(String[] args) {
            int b = 0;
            FileInputStream in = null;
            FileOutputStream ou = null;
    
            try{
                in = new FileInputStream("E:\TT.txt");
                ou = new FileOutputStream("E:\TTCopy.txt");
            } catch(FileNotFoundException e) {
                System.out.println("文件找不到");
            }
    
            try{
                while ((b = in.read()) != -1) {
                    ou.write(b);  //中文也被完成的复制了进来,且不需要加(char)强制转换?????????????????????????????
                }
                in.close();
                ou.close();
            } catch(IOException io) {
                io.printStackTrace();
            }    
        }
    }

     处理流讲解:

    1、缓冲流

    package cn.gacl.test;
    
    import java.io.*;
    public class TestBufferStream1{
        public static void main(String args[]){
            try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\java\dat.txt"));
                //在节点流FileWriter的外面再套一层处理流BufferedWriter
                String s = null;
                for(int i=0;i<100;i++){
                    s = String.valueOf(Math.random());//“Math.random()”将会生成一系列介于0~1之间的随机数。
                    // static String valueOf(double d)这个valueOf()方法的作用就是把一个double类型的数转换成字符串
                    //valueOf()是一个静态方法,所以可以使用“类型.静态方法名”的形式来调用 
                    bw.write(s);//把随机数字符串写入到指定文件中
                    bw.newLine();//调用newLine()方法使得每写入一个随机数就换行显示
                }
                bw.flush();//调用flush()方法清空缓冲区
                
            BufferedReader br = new BufferedReader(new FileReader("D:/java/dat.txt"));
                    //在节点流FileReader的外面再套一层处理流BufferedReader
                while((s = br.readLine())!=null){
                    //使用BufferedReader处理流里面提供String readLine()方法读取文件中的数据时是一行一行读取的
                    //循环结束的条件就是使用readLine()方法读取数据返回的字符串为空值后则表示已经读取到文件的末尾了。
                    System.out.println(s);
                    
                }
            bw.close();
            br.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    2、转换流

      可以把一个字节流转换成一个字符流,转换流有两种: InputStreamReader 和 OutputStreamWriter 

    import java.io.*;
    
    public class Test{
        public static void main(String[] args) {
            try {
                OutputStreamWriter or = new OutputStreamWriter(new FileOutputStream("E:\word.txt"));
                or.write("hello this is my java");
                System.out.println(or.getEncoding());//输出文档编码格式
                or.close();
    
                or = new OutputStreamWriter(new FileOutputStream("E:\word.txt", true), "ISO8859_1");//没写true,后加入的字符会替换掉已有的字符,写true后会添加到已有字符后面
                or.write("hello this is my java");
                System.out.println(or.getEncoding());
                or.close();
            } catch(Exception e) {
                System.out.println("文档不存在");
            }
    
        }
    }

    import java.io.*;
    
    public class Test{
        public static void main(String[] args) {
            try{
                InputStreamReader ow = new InputStreamReader(System.in);//System.in这里的in是一个标准的输入流,用来接收从键盘输入的数据
                BufferedReader bf = new BufferedReader(ow);
                String s = null;
                s = bf.readLine();//使用readLine()方法把读取到的一行字符串保存到字符串变量s中去
                while(s != null) {
                    System.out.println(s.toUpperCase()); //把保存在内存s中的字符串转换成大写打印出来
                    s =  bf.readLine();    //在循环体内继续接收从键盘的输入
                    if(s.equals("exit")) {//只要输入exit循环就结束,就会退出
                        break;
                    }
                }
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

    3、数据流

    4、打印流

    5、对象流

  • 相关阅读:
    tomact与eclipces的配置
    IDEA中Flink环境pom.xml文件配置
    python读写mysql
    用pyspark实现Wordcount
    python计算相似度
    Spark读取mysql
    Spark之WordCount
    IDEA中Spark环境pom.xml文件配置
    Spark之HelloWorld
    (转)二叉树的中序遍历与后序遍历构造二叉树思路(递归方法,含图片说明)
  • 原文地址:https://www.cnblogs.com/wmjlh/p/7237093.html
Copyright © 2020-2023  润新知