文本文件中有一个字符串,其中包括'\n'作为换行标志,但是从文件中读出后,似乎系统不认为\n是一个字符,这个比较麻烦,因为我在扫描字符的时候使用string.charAt(index)== '\n'来判断是否遇到了换行标志而如果这个字符串hardcode在程序中就没有问题,完全可以用:string.charAt(index) == '\n'来判断。请问如何解决?因为这个字符是需要保存在文件中的。

解决方案 »

  1.   

    用BufferedReader的readLine(),一次直接读一行,就不存在你说的问题了吧
      

  2.   

    回楼上的,这个方法没用,
    我开始用的是RandomAccessFile,然后readline,读出的字符串如下:Director:Some man \n Actor:Some man ,Some woman\n This is the introduction of the movie,This is the introduction of the movie,This is the introduction of the movieThis is the introduction of the movieThis is the introduction of the movie,This is the introduction of the movieThis is the introduction 。然后在扫描的时候,其中的\n没有被当作一个char,而是一个string,我用了BufferedReader,结果是完全一样的。:-(
      

  3.   

    用这个吧楼主
            StringTokenizer Tmp = new StringTokenizer(string, "\r\n");记住是\r\n,不是\n
      

  4.   

    或者用正则表达式判断吧!
    String a ="abc\\nad";
    Pattern pa = Pattern.compile("\\\\n");
    Matcher ma = pa.matcher(a);
    while(ma.find()){
    System.out.println(ma.group());
    }
      

  5.   

    谢谢楼上的,\r\n还是没作用,问题是为什么写在代码里的字符串中的\n 就会被当作char,而从文件中读取的就无法判断至于正则表达式,因为我是在机顶盒环境下,1。4的某些API是不支持的:-(
      

  6.   

    在window系统里面,都是回车+换行(\r\n)来做为一行的结束,而在unix下面,是直接以\n来做为一行的结束,不知你的文件是在window下创建的呢还是在unix下创建这个句子可以直接为你得到正确的换行符(不管什么操作系统):
    String lineSep = System.getProperty("line.separator")
      

  7.   

    换行记得有3个标记
    \r \n \r\n
    3个逐一判断一下把
    设置一个缓冲 碰到\r再判断后面是否是\n