做一个BufferedReader出来,用readLine()方法读取就可以了。
解决方案 »
- 请教大家一个关于JAVA虚拟机的垃圾回收站的问题
- 发布一个串口通讯的程序?
- 如何定义一个类?
- 大虾们---救救我,这个怎么回事???
- 请高手解释:抽象类中的方法声明,带有“abstract”和不带有“abstract”的区别。
- 可不可以在某个类中设置另一个类的组件的值??
- 大家有没听说最近可以学免费基础的c++
- 高层次上的问题:这样的二个类,如何设计他们之间的关系。
- 书上说servelet可以根据需要返回图片给浏览器,如何做啊
- 谁能告诉我在什么地方能找到有关classpath方面的资料????
- HttpSessionBindingListener 和HttpSessionListener有什么区别??能不能介绍一本书??谢谢!!
- 一道简单的SCJP问题
System.out.println(line);
}
br.close();
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();
这是逐行读一个文件的代码!
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上的一个例子。
你的程序报错:"ReadFile.java": cannot resolve symbol: class BufferedReader in class sms.ReadFile at line 16, column 5
你的程序报错:"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() ;
}}
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());
}