日期:2014-05-20  浏览次数:20662 次

怎么从txt中读取文件并且复制给一个字符串
数据 aaaaaa

存在e:\abc.txt中

怎么把它读入程序,并且复制给字符串data,请给出源程序。谢了

------解决方案--------------------
以前写的 你再改改http://blog.csdn.net/zqfddqr/article/details/7311646
------解决方案--------------------
what?

Java code
String fileName = "e:\\abc.tx";
RandomAcessFile raf = new RandomAccessFile(fileName, "r");
String data = raf.readLine();
raf.close();

------解决方案--------------------
Java code

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;


public class Test {

    public static void main(String[] args) throws Exception{
        
        FileInputStream fis = new FileInputStream(new File("e:/abc.txt"));
        BufferedInputStream bis = new BufferedInputStream(fis);
        String date = "";
        
        byte[] buff = new byte[1024];
        int length = 0;
        StringBuilder sb = new StringBuilder();
        while(-1 != (length = bis.read(buff))){
            sb.append(new String(buff,0,length));
        }
        date = sb.toString();
        System.out.println(date);
    }
}