Java 从文件中读取,直到指定的字符序列

Java 从文件中读取,直到指定的字符序列,java,json,file,Java,Json,File,我有一个JSON文件,如下所示: { "id":25, "type":0, "date":"Aug 28, 2017 12:14:28 PM", "isOpen":true, "message":"test" } /* some lines here, comment, not json */ 我想做的是能够从文件中读取,直到它检测到注释部分“/*”的开头 我能够编写一些代码,但由于某些原因,输出似乎不正常: BufferedReader br = null

我有一个JSON文件,如下所示:

{  
   "id":25,
   "type":0,
   "date":"Aug 28, 2017 12:14:28 PM",
   "isOpen":true,
   "message":"test"
}
/* 
some lines here, comment, not json
*/
我想做的是能够从文件中读取,直到它检测到注释部分“/*”的开头

我能够编写一些代码,但由于某些原因,输出似乎不正常:

BufferedReader br = null;
FileReader fr = null;
String comm = "/*";
fr=new FileReader(FILENAME);
br=new BufferedReader(fr);

String currentLine;

while((currentLine=br.readLine())!=null&&!(currentLine=br.readLine()).equals(comm))
{
    System.out.println(sCurrentLine);
}
br.close();
输出仅提供以下信息:

"id": 25,
"date": "Aug 28, 2017 12:14:28 PM",
我没有json部分的开头{也没有整个json消息“isOpen”,
“message”


如何读取结果并将其存储在字符串中直到注释部分?

您调用了两次
currentLine=br.readLine()
,因此读取了两行。这与人们使用

Scanner sc = new Scanner(System.in);
if (sc.nextLine() != null) // This reads a line
    myString = sc.nextLine(); //This reads the next line!
您不应该第二次调用它——直接将您的
currentLine
com
进行比较

尝试:

如果您想使用,它将类似于:

StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(FILENAME);

while (sc.hasNext())
    sb.append(sc.next());
System.out.println(sb.toString());

给你。我编辑了我的答案来使用stringbuilder(为了性能),如果你不能使用它,告诉我,我会还原它。
StringBuilder sb = new StringBuilder();
Scanner sc = new Scanner(FILENAME);

while (sc.hasNext())
    sb.append(sc.next());
System.out.println(sb.toString());