BufferedReader在用java读取我的文件时每隔一行就跳过一行
因此,我正在阅读一个文件,其中包含我在代码前面写的约会。我想筛选文本文件,找到某个日期的约会,并将它们添加到ArrayList中,但当BufferedReader遍历它时,它会跳过任何其他行。。。这是我的密码BufferedReader在用java读取我的文件时每隔一行就跳过一行,java,bufferedreader,lines,Java,Bufferedreader,Lines,因此,我正在阅读一个文件,其中包含我在代码前面写的约会。我想筛选文本文件,找到某个日期的约会,并将它们添加到ArrayList中,但当BufferedReader遍历它时,它会跳过任何其他行。。。这是我的密码 public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) { ArrayList<String> events = new ArrayList<String&g
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] split = read.split(",");
System.out.println(read);
if (split[1].equals(Integer.toString(checkDay)) && split[2].equals(Integer.toString(checkMonth)) && split[3].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return events;
}
public ArrayList读取(int-checkDay、int-checkMonth、int-checkYear){
ArrayList事件=新建ArrayList();
BufferedReader in=null;
字符串读取;
试一试{
in=新的BufferedReader(新的文件阅读器(“calendar.txt”);
而((read=in.readLine())!=null){
read=in.readLine();
String[]split=read.split(“,”);
系统输出打印项次(读取);
如果(拆分[1].equals(Integer.toString(checkDay))&&split[2].equals(Integer.toString(checkMonth))&&split[3].equals(Integer.toString(checkYear))){
添加(拆分[0]+“:“+split[1]+”/“+split[2]+”/“+split[3]);
}
}
}捕获(IOE异常){
System.out.println(“出现问题:+e);
e、 printStackTrace();
}最后{
试一试{
in.close();
}捕获(例外e){
}
}
返回事件;
}
这行代码你读了两遍
while ((read = in.readLine()) != null) { // here
read = in.readLine(); // and here
您在此处遇到错误:
while ((read = in.readLine()) != null)
read = in.readLine();
您应该将read=in.readLine()保留在while中。然后删除另一行。pl尝试此操作
您在while循环中使用了两次“read=in.readLine())”这就是它跳过洛美山脉的原因
public ArrayList<String> read(int checkDay, int checkMonth, int checkYear) {
ArrayList<String> events = new ArrayList<String>();
BufferedReader in = null;
String read;
try {
in = new BufferedReader(new FileReader("calendar.txt"));
while ((read = in.readLine()) != null) {
String[] split = read.split(",");
System.out.println(read);
if (split[0].equals(Integer.toString(checkDay)) && split[1].equals(Integer.toString(checkMonth)) && split[2].equals(Integer.toString(checkYear))) {
events.add(split[0] + " : " + split[1] + "/" + split[2] + "/" + split[3]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return events;
public ArrayList读取(int-checkDay、int-checkMonth、int-checkYear){
ArrayList事件=新建ArrayList();
BufferedReader in=null;
字符串读取;
试一试{
in=新的BufferedReader(新的文件阅读器(“calendar.txt”);
而((read=in.readLine())!=null){
String[]split=read.split(“,”);
系统输出打印项次(读取);
如果(拆分[0].equals(Integer.toString(checkDay))&&split[1].equals(Integer.toString(checkMonth))&&split[2].equals(Integer.toString(checkYear))){
添加(拆分[0]+“:“+split[1]+”/“+split[2]+”/“+split[3]);
}
}
}捕获(IOE异常){
System.out.println(“出现问题:+e);
e、 printStackTrace();
}最后{
试一试{
in.close();
}捕获(例外e){
}
}
返回事件;
答案在哪里?@DannyRadziewicz-发生了:)。不客气:)