File fileIn=new File(strFilePath); String out = DateDealer.nowStringLong().toString(); BufferedReader bfr = null; logger.info("正在读取文件" + strFilePath); try { if(fileIn.exists()) { //get BufferedReader from the file bfr=new BufferedReader(new FileReader(fileIn)); //read one line of the file and make a String String strResult; while((strResult = bfr.readLine()) != null) { //call insertData to insert the data in string //insertData(strResult); } bfr.close(); } } catch(DException ex) { logger.error("文件格式不正确"); throw ex; }
String line;
while((line = reader.readLine()) != null){
System.out.println(line);
}
就可以按原样读出整个文件
这一种方法当你对RandomAccessFile写入随意数据时。
要读出来的时候,控制它的长度,往往很有效。
以下的程序段是我在处理一个特定问题的一部分~~
但愿对你有帮助raf = new RandomAccessFile(file,"rw");
actual = raf.length();
if(actual == 0) { println("NONEWS");
return;
}
while((line = raf.readUTF())!= null) {
writer.println(line);
monitor = raf.getFilePointer();
if(actual == monitor + 2) break;
ch = raf.read();
ch = raf.read();
}
f.read(b);
If no byte is available because the stream is at end of file, the value -1 is returned
File f=new File("a.txt");
while((r=f.readLine())!=null){
....
}
byte b[]=new byte[4];
f.read(b);呢?
感觉不太好设置循环呀
String out = DateDealer.nowStringLong().toString();
BufferedReader bfr = null;
logger.info("正在读取文件" + strFilePath);
try
{
if(fileIn.exists())
{
//get BufferedReader from the file
bfr=new BufferedReader(new FileReader(fileIn));
//read one line of the file and make a String
String strResult;
while((strResult = bfr.readLine()) != null)
{ //call insertData to insert the data in string
//insertData(strResult); }
bfr.close();
}
}
catch(DException ex)
{
logger.error("文件格式不正确");
throw ex;
}