import java.io.*;
import java.util.*;
public class File_IO{
private Scanner keyboard = new Scanner(System.in);
Scanner fileIn;
PrintWriter fileOut;
public File_IO(){
}
public void openInputFile(){
boolean correctFileQ, tryAgainQ = false;
do {
correctFileQ = false;
System.out.println("Please enter the name of the input file.");
System.out.println("For example, testscores.txt: ");
try{
fileIn = new Scanner(new FileInputStream(keyboard.nextLine()));
correctFileQ = true;
}
catch (FileNotFoundException e){
System.out.println("File not found. Check file name and file path.");
System.out.println("Would you like to try another file? (y/n)");
if( !tryAgainQ ){
System.out.println("\\nTERMINATING the program!!!\\n");
System.exit(0);
}
}
} while( !correctFileQ && tryAgainQ );
}
public boolean hasInputLine(){
return fileIn.hasNextLine();
}
public String readInputLine(){
return fileIn.nextLine();
}
public void closeInputFile(){
fileIn.close();
}
public void openOutputFile(String arquivo){
boolean correctFileQ, tryAgainQ = false;
do{
correctFileQ = false;
try{
fileOut = new PrintWriter(new FileOutputStream(arquivo+".txt"));
correctFileQ = true;
}
catch (FileNotFoundException e){
System.out.println("Error opening the file. Check file name and file path.");
System.out.println("Would you like to try another file? (y/n)");
tryAgainQ = ( keyboard.nextLine().toLowerCase().charAt(0) == 'y' );
if( !tryAgainQ ){
System.out.println("\nTERMINATING the program!!!\n");
System.exit(0);
}
}
} while( !correctFileQ && tryAgainQ );
}
public void writeOutputLine(String line){
fileOut.println(line);
}
public void closeOutputFile(){
fileOut.close();
}
public int nextInt(){
return fileIn.nextInt();
}
public String nextString(){
return fileIn.next();
}
public boolean hasNextInt(){
if (fileIn.hasNextInt()){
return true;
}
else
return false;
}
public boolean hasNext(){
if(fileIn.hasNext()){
return true;
}
else
return false;
}
}
Nenhum comentário:
Postar um comentário