运用JAVA读取txt文件

来源:互联网 发布:爸爸网络用语是什么 编辑:程序博客网 时间:2024/03/29 19:34

package com.b;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
* @author dengshaohua
*/
public class Utils {

/**
* 读取数据
*/
public String ReadDate() {
    String url = "c:/data.txt";
    String strs = "";
    try {
         FileReader read = new FileReader(new File(url));
         StringBuffer sb = new StringBuffer();
         char ch[] = new char[1024];
         int d = read.read(ch);
         while(d!=-1){
             String str = new String(ch,0,d);
            sb.append(str);
            d = read.read(ch);
          }
          System.out.print(sb.toString());
          String a = sb.toString().replaceAll("@@@@@", ",");
          strs = a.substring(0,a.length()-1);
     } catch (FileNotFoundException e) {
          e.printStackTrace();
     } catch (IOException e) {
        e.printStackTrace();
     }
     return strs ;
}

public static void main(String a[])
{
Utils util = new Utils();
String cc = util.ReadDate();
System.out.println("result...."+cc);
}

}

原创粉丝点击