English 中文(简体)
阅读和比较档案中的特性和特征
原标题:Reading and comparing the characters and strings from file
  • 时间:2012-04-22 05:47:13
  •  标签:
  • java

The code below reads from a file helpFile.txt and checks for an index represented by # . The variable read from the file is stored in the integer c and compared with # if the read character is # without casting the integer into character. I want to know if the comparison is valid as the compiler is not showing any error. Also, suppose # is found by the program in the file and a string called topic immediately follows # and it is read using readLine(). Will the String info = br.readLine() be just topic or # + topic ? Sorry for such a lengthy question. Help much appreciated.

boolean help 内容:

    private BufferedReader br;
    private String info, topic;
    private static PrintWriter pw = new PrintWriter(System.out, true);
    int c;


        br = new BufferedReader(new FileReader("helpFile.txt"));

    try{
        do{
            //read characters until  #  is found
            c = br.read();


            if(***c== # ***){   //check if the character is  # 
                pw.println(c);
                if((**topic=br.readLine()**).equalsIgnoreCase(what)){   //check if  what  string is equal to  s  which is the string after  # 
                    while((info=br.readLine())!=null){  //print info until the end of info
                        if(info!=null)
                            pw.println(info);
                    }
                    return true;
                }
            }
        }
        while(c!=-1);
    }
    catch(IOException ex){
        pw.println("File error.");
        ex.printStackTrace();
        try{
            br.close();
        }
        catch(IOException e){
            pw.println("Error closing file.");
            e.printStackTrace();
        }
        return false;
    }

    try{
        br.close();
    }
    catch(IOException ex){
        pw.println("Error closing file.");
        ex.printStackTrace();
    }
    return false; //topic not found
}
最佳回答

我尝试了你的法典,正在与我一道打上罚款,我认为需要检查你的“助手”。 i 在此处使用。

adad#hello

• 青少年

and this is the outout i am getting.

c) 35

专题: hello

info: • 青少年

i printed all the three vars u used. c, topic, info. Now since you are using readline() after reading a character, u must give your "info" from next line in "helpFile.txt"

info will contain anything after topic, as you are using readline() function, it will go to the next line. try with my example.

碰到“第2号”后,你就会var。

C将具有“第”(35)。

当时

专题在“第2号”之后至“第2条”结束,因为阅读了(a)项;

当时

info will have the next line after topic.

如果你安排你的帮助。 适当打字,将处以罚款。

http://www.ohchr.org。

(一) 每一时间都必须注明全案名称

You are using eclipse, and you are saving the file in the "SRC" folder i guess. Save them in your Project folder. just one above the SRC folder and 当时 do this.

br = new BufferedReader(new FileReader("helpFile.txt"));

它应当发挥作用。

EDIT2 you don t need to check info for null twice

while((info=br.readLine())!=null){
//print info until the end of
//  if(info!=null) noT needed, u alreay did that above
pw.println("info"+info);
             }

如果是民族解放军,它就会自动出院。

EDIT3

i 不想印刷所有文本

既然你用了标号来标示这块块块的开始,你就可以用任何东西标出尾。 页: 1

helpFile.txt

adad#hello

• 青少年

美元

此处的其他案文

b 真主党

...

现在,你可以修改如下内容:

while(!(info=br.readLine()).equals("美元")){
      pw.println("info"+info);
                            }

The loop will exit as soon as it gets "美元", and it won t print anything after that.

问题回答

You may wanna read the javadoc for the class you are using (BufferedReader), it can be found here, but i think it s ok to compare the characters with == because a char is really a numeric type. But i suggest you read more about enconding, because i think FileReader will use the default plataform encoding to read your file and that may not be your file encoding. It s always good practice to inform the encoding of the file. About the second question, acoording to javadoc (again), it will read the entire line (not from the point you read the char). Hope that helps, i m goona try to run this later to see the results





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签