안녕 나는이 자바 코드, 처음에 쓸 수있는 파일의 이름을 묻는 있도록 Scanner 클래스와 Java에서 next() 메서드를 사용하는 방법은 무엇입니까?
import java.io.*;
import java.util.*;
public class SongWriter
{
public static void main(String[] args)
{
PrintWriter outputStream = null; // Scope must be outside the try/catch structure
try
{
outputStream = new PrintWriter("Song.txt"); // new
FileOutputStream("Song.txt")
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file Song.txt.");
System.exit(0);
}
System.out.println("\n classical songs has many lines");
System.out.println("\nNow enter the three lines of your Song.");
String line = null;
Scanner keyboard = new Scanner(System.in);
int count;
for (count = 1; count <= 3; count++)
{
System.out.println("\nEnter line " + count + ": ");
line = keyboard.nextLine();
outputStream.println(count + "\t" + line);
}
outputStream.close();
System.out.println("\nYour Song has been written to the file Song.txt.\n");
} // end of main
} // end of class
가 어떻게 프로그램을 조정 할 수 있습니다. Scanner 클래스와 next() 메서드를 사용하십시오. 파일 이름을 접미사 .txt로 끝나야한다는 것을 독자에게 알린 후 문자열 변수로 파일 이름을 읽습니다. 예 : - 파일 이름이 Haiku1.txt, Haiku2.txt 및 Haiku3.txt 인 노래.
공백을 추가하여 더 읽기 쉽게 만들면 매우 유용합니다. – Deena
Scanner 클래스의 Javadocs를 읽었습니까? –
@Deena 안녕하세요, 죄송합니다. – Sameena