似乎有不同的方法来读取和写入Java中的文件数据.
我想从文件中读取ASCII数据.可能的方式和差异是什么?
据我所知,从Java文件中读取基于字符的数据的两种最常用的方法是使用Scanner或BufferedReader.我也知道BufferedReader通过使用缓冲区来有效地读取文件以避免物理磁盘操作.我的问题是:
Scanner执行以及BufferedReader?Scanner,BufferedReader反之亦然?我有一个java服务器应用程序,下载CSV文件并解析它.解析可能需要5到45分钟,并且每小时发生一次.这种方法是应用程序的瓶颈,因此它不是过早的优化.到目前为止的代码:
client.executeMethod(method);
InputStream in = method.getResponseBodyAsStream(); // this is http stream
String line;
String[] record;
reader = new BufferedReader(new InputStreamReader(in), 65536);
try {
// read the header line
line = reader.readLine();
// some code
while ((line = reader.readLine()) != null) {
// more code
line = line.replaceAll("\"\"", "\"NULL\"");
// Now remove all of the quotes
line = line.replaceAll("\"", "");
if (!line.startsWith("ERROR"){
//bla bla
continue;
}
record = line.split(",");
//more error handling
// build the object and put it in HashMap …Run Code Online (Sandbox Code Playgroud)