[英]What's the best way to determine the total number of words of a file in Java?
[英]What's best way to use delim in Java IO from File
我的输入文件格式如下。
ders : bilgisayargiris
hoca : erdogan
kod : 101
akts : 5
gtukred : 3
donem : 1
info : bu derste bilgisayar falan .Also ogretiliyor confusing,blah,words,blah
Also ogretiliyor someshit.
soru : flip-flop devre nedir
cevap : erdoz
-
基本上我正在读取一个这样填充的txt文件,获取冒号的右侧并将其分配给我在课堂上的数据。 字符“-”用作指示文件正在寻找另一个信息结构/片段的指示符。
这是我在下面苦苦挣扎的部分代码。
public void read(){
className = this.getClass().getSimpleName();
className = className + ".txt";
openFile(className);
readFile();
System.out.println(className);
closeFile();
}
public void openFile(String filer){
try{
scan = new Scanner(new File("/home/paypaytr/IdeaProjects/yallah/src/a.txt")); //test purposes
}
catch (Exception e){
System.out.println(className+"couldnt found.");
//some safe quit mechanicsm
}
}
public void readFile(){
while (scan.hasNextLine()){
if(scan.nextLine()=="ders"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
name = scan.nextLine();
}
else if(scan.nextLine()=="hoca"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
tutor = scan.nextLine();
}
else if(scan.nextLine()=="kod"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
code = scan.nextInt();
}
else if(scan.nextLine()=="akts"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
akts = scan.nextInt();
}
else if(scan.nextLine()=="gtukred"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
gtukred = scan.nextInt();
} else if(scan.nextLine()=="donem"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
donem = scan.nextInt();
}
else if(scan.nextLine()=="info"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
while(scan.nextLine()!="soru")
info += scan.nextLine();
}
else if(scan.nextLine()=="soru"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
soru = scan.nextLine();
}
else if(scan.nextLine()=="cevap"){
scan.nextLine(); //this is for skipping ":" but would love to delim it.
cevap = scan.nextLine();
}
}
}
public void closeFile(){
scan.close();
}
有人可以帮忙吗?
您可以尝试使用:带有readLine
BufferedReader
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) //Checks what in C++ you do EOF
{
line = line.substring(line.indexOf(":")+1); //Here I am just implementing, how to print string after ":", you can process the string stored in variable line as per your convenience
System.out.print(line);
}
br.close();
fr.close();
}
}
我希望您有主意,可以接受答案,如果有帮助,请关闭问题。
您可以尝试FileReader
String p = ":";
FileReader fr = new FileReader(new File("C:\\raw.txt"));
BufferedReader br = new BufferedReader(fr);
long startT = System.currentTimeMillis();
String val = null;
String[] finalAr = new String[1048576];
while(br.ready()){
finalAr[n] = br.readLine();
n++;
}
for(int i = 1; i < finalAr.length; i++){
if(finalAr[i].contains(p)) {
System.out.println(finalAr[i].split(",")[1]);
}
}
我建议您将其简化。
完整地读取每一行作为一个字符串。 将行存入内存后,处理标记化和解析。
您可以在第一个字符中查找“#”,而忽略后面的注释。
您可以在第一个字符中查找“-”以表示新的结构。
您可以忽略空白行。
您可以使用String.split(“:”)方法拆分附带的所有内容,以获取键/值对。
您还应该考虑一些易于理解的东西,例如.properties文件,YAML或JSON。 为什么要发明一个新的?
我建议您使用实现readLine()方法的BufferedReader类,这样可以简化您的工作。
这里的例子。
然后可以使用字符串对象的split(":")
方法split(":")
读取的行。
这里的例子。
默认情况下,扫描程序使用空格分隔令牌。 您必须设置另一个定界符: scan.useDelimiter("\\\\s*:\\\\s*")
接下来的一点是,您通常无法将字符串与==进行比较,必须使用equals()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.