📄 du.txt
字号:
BufferedReader in = new BufferedReader (new FileReader("C:/abc.txt"));
String s;
while((s = in.readLine()) != null)
{
//自己处理s
}Top
stopwords = new Vector();
FileReader fr = new FileReader("english.stop.txt");
BufferedReader br = new BufferedReader(fr);
while ((temp= br.readLine()) != null) {
stopwords.add(temp);
}Top
另外一个方法,要jdk1.5
import java.util.*;
Scanner cin=new Scanner(new InputStream("c:\\in.txt"));
while(cin.hasNext())
{
String s=cin.nextLine();
........
}
import java.io.*;
public class test {
public test() {
}
public static void main(String[] args) throws Exception {
String filename = "c://1.txt";
FileReader fr = new FileReader(filename);
int ch = 0;
while ( (ch = fr.read()) != -1)
{
System.out.print( (char) ch);
}
}
}Top
主要是读出来之后怎么分解的问题吧,还有中文的问题,中文应该使用UTF-8格式,用下面的方法来读可以解决中文问题
public String getString()
{
String str = "";
InputStream in = this.getClass().getResourceAsStream("/res/data.txt");
int c;
ByteArrayOutputStream bais = new ByteArrayOutputStream();
try {
while((c = in.read())!= -1)
{
bais.write(c);
}
byte data[] = bais.toByteArray();
str = new String(data,"utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
我给你写个类吧,是把.txt文件中的文字传给一个字符串对象,你自己看看,看不懂就问我!
import java.io.*;
public class justTest
{
public static void main(String[] args)
{
int arrlen = 10000;
byte[] infile = new byte[arrlen];
try
{
FileInputStream fis = new FileInputStream("Proxy.txt");//这里写上你的文件名,记得是当前目录的
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
try
{
int filelength = dis.read(infile);
String filestring = new String(infile, 0, filelength);
for(int i = 0; i < filestring.length(); i++)
{
if(filestring.charAt(filestring.length()-1) == '\r')
{
System.out.println("......OK."+i+".....");
break;
}
}
//System.out.println(filestring);
}
catch(IOException iox)
{
System.out.println("File read error...");
iox.printStackTrace();
}
}
catch (FileNotFoundException fnf)
{
System.out.println("File not found...");
fnf.printStackTrace();
}
}
}
读取文件的内容,并将原样输出至屏幕上
使用方法:java Readfile 文件名
*/
import java.io.*;
public class Readfile
{
public static void main(String[] args)
{
byte[] buff = new byte[1024];
boolean cont = true;
FileInputStream infile = null;
// 生成对象infile 准备读取文件
try
{
infile = new FileInputStream(args[0]);
}
catch (FileNotFoundException e)
{
System.err.println("没有找到文件");
System.exit(1);
}
while (cont)
{
try
{
int n = infile.read(buff); // 从文件读取数据
System.out.write(buff, 0, n); // 写入System.out中
}
catch (Exception e)
{
cont = false;
}
}
try
{
infile.close();
}
catch (IOException e)
{
System.err.println("文件错误");
System.exit(1);
}
}
}
我以前看别人的,你试试
public String myReadLine (int lineNumber) {
String testFileName = "/mybook.txt";//测试的文本文件名
ByteArrayOutputStream s;
InputStream in;
int pageWords;
int k=0;
// 一行文本是以行结束标志(\n or \r)为终止的一行字符 一般是'\r'0D(13)后'\n'0A(10)
pageWords = wordOfPage;
in = this.getClass().getResourceAsStream(testFileName);
s = new ByteArrayOutputStream();
try{
int ch=0;
int i=0;
while(i < (lineNumber - 1) * pageWords){ //跳过前面的n-1页
ch = in.read();
if(ch !=13 && ch!=10){
if (ch>=127 && ch<=254){ //中文
k++;
if(k==2){
i++;
k=0;
}
}else{ //西文和数字
i++;
}
}
}
i=0;
k=0;
while ( i< pageWords) {
ch=in.read();
if(ch==-1){ //文件结尾
currentPage = 0;
}
if(ch !=13 && ch!=10){
s.write( ch );
if (ch>=127 && ch<=254){ //中文
k++;
if(k==2){
i++;
k=0;
}
}else{ //西文和数字
i++;
}
}
}
in.close();
}catch (IOException ioe){
System.out.println(ioe.toString());
}
String str = s.toString();
try{
s.close();
}catch (IOException ioe){
System.out.println(ioe.toString());
}
return str.trim();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -