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();
// 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上的一个例子。
TO ChDw(米): 你的程序报错:"ReadFile.java": cannot resolve symbol: class BufferedReader in class sms.ReadFile at line 16, column 5
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() ; }}
读取目录下的文件可以用File类的listFiles()方法 File path = new File("c:\\windows\\");
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());
}