• Java 读写文件


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class MainClass
    {
            public static void main(String[] args)
            {

            }
            /**
             * 使用FileWriter类写文本文件
             */
            public static void writeMethod1()
            {
                    String fileName="C:\kuka.txt";
                    try
                    {
                            //使用这个构造函数时,如果存在kuka.txt文件,
                            //则先把这个文件给删除掉,然后创建新的kuka.txt
                            FileWriter writer=new FileWriter(fileName);
                            writer.write("Hello Kuka: ");
                            writer.write("  My name is coolszy! ");
                            writer.write("  I like you and miss you。");
                            writer.close();
                    } catch (IOException e)
                    {
                            e.printStackTrace();
                    }
            }
            /**
             * 使用FileWriter类往文本文件中追加信息
             */
            public static void writeMethod2()
            {
                    String fileName="C:\kuka.txt";
                    try
                    {
                            //使用这个构造函数时,如果存在kuka.txt文件,
                            //则直接往kuka.txt中追加字符串
                            FileWriter writer=new FileWriter(fileName,true);
                            SimpleDateFormat format=new SimpleDateFormat();
                            String time=format.format(new Date());
                            writer.write(" "+time);
                            writer.close();
                    } catch (IOException e)
                    {
                            e.printStackTrace();
                    }
            }
            //注意:上面的例子由于写入的文本很少,使用FileWrite类就可以了。但如果需要写入的
            //内容很多,就应该使用更为高效的缓冲器流类BufferedWriter
            /**
             * 使用BufferedWriter类写文本文件
             */
            public static void writeMethod3()
            {
                    String fileName="C:/kuka.txt";
                    try
                    {
                            BufferedWriter out=new BufferedWriter(new FileWriter(fileName));
                            out.write("Hello Kuka:");
                            out.newLine();  //注意 不一定在各种计算机上都能产生换行的效果
                            out.write("  My name is coolszy! ");
                            out.write("  I like you and miss you。");
                            out.close();
                    } catch (IOException e)
                    {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
            }
            /**
             * 使用FileReader类读文本文件
             */
            public static void readMethod1()
            {
                    String fileName="C:/kuka.txt";
                    int c=0;
                    try
                    {
                            FileReader reader=new FileReader(fileName);
                            c=reader.read();
                            while(c!=-1)
                            {
                                    System.out.print((char)c);
                                    c=reader.read();
                            }
                            reader.close();
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }

            /**
             * 使用BufferedReader类读文本文件
             */
            public static void readMethod2()
            {
                    String fileName="c:/kuka.txt";
                    String line="";
                    try
                    {
                            BufferedReader in=new BufferedReader(new FileReader(fileName));
                            line=in.readLine();
                            while (line!=null)
                            {
                                    System.out.println(line);
                                    line=in.readLine();
                            }
                            in.close();
                    } catch (IOException e)
                    {
                            e.printStackTrace();
                    }
            }
    }

    ============================================================

    按行读取

    //读取文件(字符流)
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\1.txt"),"GBK"));#这里主要是涉及中文
            //BufferedReader in = new BufferedReader(new FileReader("d:\1.txt")));
            //写入相应的文件
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\2.txt"),"GBK"));
            //BufferedWriter out = new BufferedWriter(new FileWriter("d:\2.txt"));
            //读取数据
            //循环取出数据
            String str = null;
            while ((str = in.readLine()) != null) {
                System.out.println(str);
                //写入相关文件
                out.write(str);
                out.newLine();
            }
            
            //清楚缓存
            out.flush();
            //关闭流
            in.close();
            out.close();

    ============================================================

    按行读取

    package com.shaonaiyi.local;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;

    /**
     * Java实现本地读文件
     */
    public class ReadFile {

        public static void main(String[] args) throws Exception{

            String path = "/Users/shaonaiyi/datas/tmp/hello.txt";
            //win系统
    //        String path = "c:\hello.txt";
            FileInputStream fileInputStream = new FileInputStream(path);

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));

            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }

            fileInputStream.close();

        }
    }

    ============================================================

    REF

    https://blog.csdn.net/jiangxinyu/article/details/7885518

    https://www.cnblogs.com/qianbi/p/3378466.html

    https://blog.csdn.net/shaock2018/article/details/90607893

    https://www.cnblogs.com/zhuocheng/articles/2285290.html

  • 相关阅读:
    1833: [ZJOI2010]count 数字计数——数位dp
    【模板】BZOJ 3685: 普通van Emde Boas树——Treap
    【模板】解决二分图匹配的强力算法——Hopcroft-Karp算法
    BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
    【模板】二分图匹配/一般图匹配——匈牙利算法/随机匈牙利算法
    【模板】BZOJ 1692:队列变换—后缀数组 Suffix Array
    BZOJ 4241: 历史研究——莫队 二叉堆
    【模板】BZOJ 3781: 小B的询问 莫队算法
    BZOJ 3656: 异或 (组合数取模 CRT)
    【模板】SPOJ FACT0 大数分解 miller-rabin & pollard-rho
  • 原文地址:https://www.cnblogs.com/emanlee/p/13970998.html
Copyright © 2020-2023  润新知