파일을 대체하려고하는데 사용자 입력에 따라 일부 줄을 그대로 유지합니다. (코드 아래 세부 정보)
public class RemoveLines {
public static void main(String[] args)
throws FileNotFoundException {
// prompt for input file name
Scanner console = new Scanner(System.in);
System.out.print("Type first file name to use: ");
String filename1 = console.nextLine();
System.out.print("Type second file name to use: ");
String filename2 = console.nextLine();
System.out.println("enter an integer: ");
int n = console.nextInt();
Scanner input = new Scanner(new File(filename1)); //put the first file as input
PrintStream output = new PrintStream(new File(filename2)); //put the second file as output
int count =0;
while(input.hasNextLine()){
count ++;
while(n<=count){
output.println(); // this is where i don't know what to place
}
}
}
}
프로그램은 사용자에게 2 개의 파일 이름과 정수 n을 입력하도록 프롬프트해야합니다. 첫 번째 파일의 처음 n 줄을 포함하는 두 번째 파일을 생성해야합니다. 첫 번째 파일에 n 줄 미만이 포함 된 경우 두 번째 파일에는 첫 번째 파일의 모든 줄이 포함됩니다. while 루프를 작성하기 시작했지만 원하는 출력을 얻으려면 어떤 명령을 포함해야할지 모르겠습니다. 감사합니다.
다음 코드를 사용해보십시오.
int count = 0;
while (count < n) {
if (input.hasNextLine()) {
output.println(input.nextLine()); // this is where i don't know what to place
count++;
}else
{
break;
}
}
/**
* Close scanner
*/
input.close();
console.close();
output.close();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다