什么';在Java IO中从文件使用delim的最佳方法

什么';在Java IO中从文件使用delim的最佳方法,java,file,io,Java,File,Io,我有一个格式如下的输入文件 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 - 基本上,我

我有一个格式如下的输入文件

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();
}

有人能帮忙吗?

您可以尝试使用:
BufferedReader
readLine

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 p=“:”;
FileReader fr=新的FileReader(新文件(“C:\\raw.txt”);
BufferedReader br=新的BufferedReader(fr);
long startT=System.currentTimeMillis();
字符串val=null;
字符串[]finalAr=新字符串[1048576];
while(br.ready()){
finalAr[n]=br.readLine();
n++;
}
对于(int i=1;i
我建议您使用实现readLine()方法的类,这将简化您的工作

例子是对的

然后可以使用string对象的
split(:”
方法拆分读取的行


正确的例子。

我建议您简化这个过程

将每一行作为字符串整体阅读。在内存中有行之后,处理标记化和解析

您可以在第一个字符中查找“#”,忽略后面的注释

您可以在第一个字符中查找“-”,以指示新结构

您可以忽略空行

您可以使用String.split(“:”)方法拆分其他任何附带的内容,以获取键/值对


<>你也应该考虑一些很好理解的东西,比如属性文件、YAML或JSON。为什么要发明一个新的标记?

默认情况下,扫描程序使用空白来分隔标记。您必须设置另一个分隔符:
scan.useDelimiter(“\\s*:\\s*”)


下一点是,您通常无法将字符串与==进行比较,您必须使用
equals()

请尝试以某种方式编写问题:您到底在做什么?有时较长的问题会让人望而却步。寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用“编辑”链接改进您的问题-不要通过评论添加更多信息。谢谢确切地这基本上是再次询问“如何重新发明”java属性。唯一的区别是使用:而不是=我实际上是第一次听到这些java方法/文件类型。如果可能的话,我喜欢用我自己的作品来代替图书馆。感谢您的帮助,我们将尝试修复代码。您希望自己尝试一些东西并学习该语言的工作原理,这是很好的,但一旦您熟悉了,就不应该对库嗤之以鼻。您将想要了解所有这些文件格式。如果不了解JSON,就无法进行web开发。属性文件已经内置到您正在使用的JVM中。为什么不先试试看你怎么想?尽管我在异常抛出方面有问题。那么,您不能在方法中声明异常吗?因为我有一个公共的void Read函数,它应该抛出异常。我不在main上编写代码。如果您在EclipseIDE中编写代码,那么它将为您提供编辑/声明异常的建议。