java从从控制台输入只从第二行开始读

public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException{
System.out.println("输入内容:");
Scanner console=new Scanner(System.in);
String str=console.nextLine();
RandomAccessFile raf=new RandomAccessFile("test.txt","rw");
while((str=console.nextLine())!=null){
raf.write(str.getBytes("gbk"));
if(str.equals("exit")){
break;
}
}
raf.close();
}
}

加一个变量控制就可以了:
int count=0;
while((str=console.nextLine())!=null){

if(count==0){
continue;//这样的话,第一次读取的话就从这儿返回了,然后就是第二行了}
count=1;
……追问

还是不行。而且加上过后输入exit不会停止

追答

public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException{
System.out.println("输入内容:");
Scanner console=new Scanner(System.in);
String str=console.nextLine();
RandomAccessFile raf=new RandomAccessFile("test.txt","rw");
int count=0;
while((str=console.nextLine())!=null){
if(count==0){
countinue;
}
count++;
raf.write(str.getBytes("gbk"));
if(str.equals("exit")){
break;
}
}
raf.close();
}
}

温馨提示:答案为网友推荐,仅供参考
相似回答