zoukankan      html  css  js  c++  java
  • Java读取txt文件,换行写txt文件

     Java读取txt文件,换行写txt文件

    1. Java读取txt文件

    package com.campu;
     
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
     
    /**
     * H20121012.java
     */
    public class H20121012 {
      
        public static void readTxtFile(String filePath){
            try {
                    String encoding="GBK";
                    File file=new File(filePath);
                    if(file.isFile() && file.exists()){ //判断文件是否存在
                        InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                        BufferedReader bufferedReader = new BufferedReader(read);
                        String lineTxt = null;
                        while((lineTxt = bufferedReader.readLine()) != null){
                            System.out.println(lineTxt);
                        }
                        read.close();
            }else{
                System.out.println("找不到指定的文件");
            }
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
         
        }
         
        public static void main(String argv[]){
            String filePath = "L:\Apache\htdocs\res\20121012.txt";
    //      "res/";
            readTxtFile(filePath);
        }
         
         
     
    }

    2.不同操作系统换行写txt文件

    针对常用的系统,可以使用如下的转义符实现换行:
    windows下的文本文件换行符:
    linux/unix下的文本文件换行符:
    Mac下的文本文件换行符:  

    (1).使用java中的转义符" ":

    Java代码  

    String str="aaa";  

    str+=" ";  

    (2).BufferedWriter的newline()方法:

    Java代码  

    FileOutputStream fos=new FileOutputStream("c;\11.txt");  

    BufferedWriter bw=new BufferedWriter(fos);  

    bw.write("你好");  

    bw.newline();  

    bw.write("java");  

    w.newline();   

  • 相关阅读:
    asp.net后台导出excel的方法:使用response导出excel
    asp.net后台导出excel的方法一:使用response导出excel
    infragistics--web网站升级注意点
    infragistics--网站部署时webtab的tab前出现textbox
    Jewels and Stones
    To Lower Case
    Unique Email Addresses
    unique-morse-code-words
    很久没来博客园了。。。。
    Centos7 基础知识---------root文件夹下没有.ssh文件
  • 原文地址:https://www.cnblogs.com/harry335/p/4511794.html
Copyright © 2011-2022 走看看