读取文本文件,然后解析,可以使用正则表达式提取特定串。 File file = new File(path); FileInputStream fileIns = new FileInputStream(file); java.io.InputStreamReader isReader = new java.io.InputStreamReader(fileIns) java.io.BufferedReader br = new java.io.BufferedReader(isReader);然后使用br.readLine()就可以一行一行读取文本文件。
我写的函数: import java.io.*; static final int BUFSIZE = 102400; public static InputStream InputStreamFromFile(String path) throws Exception{ File f = new File(path); if (f.exists() == false){ throw new Exception(path + " not exists!"); } return new FileInputStream(f); } public static String getStrFromIS(InputStream is) throws Exception{ ByteArrayOutputStream os = new ByteArrayOutputStream(); int read; byte[] bytes = new byte[BUFSIZE]; while ((read = is.read(bytes)) != -1) { os.write(bytes, 0, read); } return new String(os.toByteArray()); } =================== 调用 String str = getStrFromIS(InputStreamFromFile("c:\test.txt")); 这时str的内容就是"12334afasf我爱你死我活无可厚非" 你调用String相关的函数就可以取str内的任意内容,具体查看API帮助.
File file = new File(path);
FileInputStream fileIns = new FileInputStream(file);
java.io.InputStreamReader isReader = new java.io.InputStreamReader(fileIns)
java.io.BufferedReader br = new java.io.BufferedReader(isReader);然后使用br.readLine()就可以一行一行读取文本文件。
import java.io.*;
static final int BUFSIZE = 102400; public static InputStream InputStreamFromFile(String path) throws Exception{
File f = new File(path);
if (f.exists() == false){
throw new Exception(path + " not exists!");
}
return new FileInputStream(f);
}
public static String getStrFromIS(InputStream is) throws Exception{
ByteArrayOutputStream os = new ByteArrayOutputStream();
int read;
byte[] bytes = new byte[BUFSIZE];
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
return new String(os.toByteArray());
} ===================
调用
String str = getStrFromIS(InputStreamFromFile("c:\test.txt"));
这时str的内容就是"12334afasf我爱你死我活无可厚非"
你调用String相关的函数就可以取str内的任意内容,具体查看API帮助.