开发者社区 问答 正文

怎么用java读取txt文件

怎么用java读取txt文件 怎么用java读取txt文件 怎么用java读取txt文件

展开
收起
蛮大人123 2016-02-26 14:34:58 2470 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    示例代码:

    public class cin_txt {
        static void main(String args[]) {
            try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
    
                /* 读入TXT文件 */
                String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
                File filename = new File(pathname); // 要读取以上路径的input。txt文件
                InputStreamReader reader = new InputStreamReader(
                        new FileInputStream(filename)); // 建立一个输入流对象reader
                BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
                String line = "";
                line = br.readLine();
                while (line != null) {
                    line = br.readLine(); // 一次读入一行数据
                }
    
                /* 写入Txt文件 */
                File writename = new File(".\\result\\en\\output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件
                writename.createNewFile(); // 创建新文件
                BufferedWriter out = new BufferedWriter(new FileWriter(writename));
                out.write("我会写入文件啦\r\n"); // \r\n即为换行
                out.flush(); // 把缓存区内容压入文件
                out.close(); // 最后记得关闭文件
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    2019-07-17 18:48:14
    赞同 展开评论
问答分类:
问答地址: