如何逐行读取txt文件中的字符串?

captivate 2004-09-30 03:47:16
如题。。。。。。
...全文
2284 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqq 2004-10-01
  • 打赏
  • 举报
回复
http://blog.csdn.net/cqq/archive/2004/09/23/114062.aspx
wanghaijun240 2004-10-01
  • 打赏
  • 举报
回复
用两个流接在一起 一个FileInputStream流,一个bufferReader流
dpyrq 2004-10-01
  • 打赏
  • 举报
回复
readLine()
zgysc 2004-09-30
  • 打赏
  • 举报
回复
利用listFiles()把目录下所有的文件列出,再读出每一个文件的内容;
ChDw 2004-09-30
  • 打赏
  • 举报
回复
import java.io.*; 你当然需要import相应的包啊
ToAble 2004-09-30
  • 打赏
  • 举报
回复
这么多人抢着回答,呵呵,不错。
Baal_wu 2004-09-30
  • 打赏
  • 举报
回复
读取目录下的文件可以用File类的listFiles()方法
File path = new File("c:\\windows\\");

File[] fileName = path.listFiles();

System.out.println(fileName.length);
for(int i=0;i<fileName.length;i++)
{
System.out.println(fileName[i].getName());
}
captivate 2004-09-30
  • 打赏
  • 举报
回复
TO ChDw(米):
你的程序报错:"ReadFile.java": cannot resolve symbol: class BufferedReader in class sms.ReadFile at line 16, column 5


#这是我copy你的程序后的写了一个测试:
public class ReadFile {
public ReadFile() {
}
public void readfile() throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("E:/netMAC.txt")));
for(String line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
}
br.close();
}

public static void main(String[] args) {
ReadFile rf = new ReadFile();
rf.readfile() ;
}

}
captivate 2004-09-30
  • 打赏
  • 举报
回复
TO ChDw(米):
你的程序报错:"ReadFile.java": cannot resolve symbol: class BufferedReader in class sms.ReadFile at line 16, column 5
yuanzi 2004-09-30
  • 打赏
  • 举报
回复
// Servlet imports
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContext;
// IO imports
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;


public class InitializeProductList implements ServletContextListener {

public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String catalogFileName = context.getInitParameter("catalogFileName");
InputStream is = null;
BufferedReader catReader = null;

try {
is = context.getResourceAsStream(catalogFileName);
catReader = new BufferedReader(new InputStreamReader(is));
String productString;
ProductList catalog = new ProductList();

// Parse the catalog file to construct the product list
while ( (productString = catReader.readLine()) != null ) {
StringTokenizer tokens = new StringTokenizer(productString, "|");
String code = tokens.nextToken();
String price = tokens.nextToken();
String quantityStr = tokens.nextToken();
int quantity = Integer.parseInt(quantityStr);
String description = tokens.nextToken();
Product p = new Product(code, price, quantity, description);
catalog.addProduct(p);
}

// Store the catalog as an application-scoped attribute
context.setAttribute("catalog", catalog);

context.log("The ProductList has been initialized.");

// Log any exceptions
} catch (Exception e) {
context.log("Exception occured while parsing catalog file.", e);

// Clean up resources
} finally {
if ( is != null ) {
try { is.close(); } catch (Exception e) {}
}
if ( catReader != null ) {
try { catReader.close(); } catch (Exception e) {}
}
}
}

public void contextDestroyed(ServletContextEvent sce) {
// TODO: rewrite the catalog file
}
}
这是sl314上的一个例子。
zenithliu 2004-09-30
  • 打赏
  • 举报
回复
FileReader fr = new FileReader(str);// 文件名
BufferedReader bufferedreader = new BufferedReader(fr);
String instring;
while ( (instring = bufferedreader.readLine()) != null) {
String sss = instring.replaceAll(" ","");
if(sss.length() != 0)
{
System.out.println(instring);
}
}
fr.close();
这是逐行读一个文件的代码!
Ronanljy 2004-09-30
  • 打赏
  • 举报
回复
米兄强!
captivate 2004-09-30
  • 打赏
  • 举报
回复
现在,我只知道某个目录下有txt文件,文件名是未知的,我怎么去逐一读取该目录下的很多txt文件?
ChDw 2004-09-30
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:/a.txt")));

for(String line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(line);
}
br.close();
captivate 2004-09-30
  • 打赏
  • 举报
回复
没做过,可否给一段程序?
buaaaladdin 2004-09-30
  • 打赏
  • 举报
回复
做一个BufferedReader出来,用readLine()方法读取就可以了。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧