Projeto - Código File_IO.java
Autor: Projeto Arduino PUC-SP | Publicado: 2012-06-11T08:30:00-03:00

import java.io.*;
import java.util.*;
&nbsp; &nbsp;&nbsp;

public class File_IO{
&nbsp; &nbsp; private Scanner keyboard = new Scanner(System.in); &nbsp; &nbsp;
&nbsp; &nbsp; Scanner fileIn; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; PrintWriter fileOut;&nbsp;
&nbsp; &nbsp; public File_IO(){
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; public void openInputFile(){
&nbsp; &nbsp; &nbsp; &nbsp; boolean correctFileQ, tryAgainQ = false;
&nbsp; &nbsp; &nbsp; &nbsp; do {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctFileQ = false;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter the name of the input file.");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("For example, testscores.txt: ");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileIn = new Scanner(new FileInputStream(keyboard.nextLine()));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctFileQ = true;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (FileNotFoundException e){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("File not found. Check file name and file path.");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Would you like to try another file? (y/n)");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( !tryAgainQ ){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\\nTERMINATING the program!!!\\n");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; } while( !correctFileQ &amp;&amp; tryAgainQ );
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public boolean hasInputLine(){
&nbsp; &nbsp; &nbsp; &nbsp; return fileIn.hasNextLine();
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public String readInputLine(){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return fileIn.nextLine();
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public void closeInputFile(){
&nbsp; &nbsp; &nbsp; &nbsp; fileIn.close();
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public void openOutputFile(String arquivo){
&nbsp; &nbsp; &nbsp; &nbsp; boolean correctFileQ, tryAgainQ = false;
&nbsp; &nbsp; &nbsp; &nbsp; do{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctFileQ = false;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileOut = new PrintWriter(new FileOutputStream(arquivo+".txt"));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctFileQ = true;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (FileNotFoundException e){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error opening the file. Check file name and file path.");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Would you like to try another file? (y/n)");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tryAgainQ = ( keyboard.nextLine().toLowerCase().charAt(0) == 'y' );
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( !tryAgainQ ){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nTERMINATING the program!!!\n");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; } while( !correctFileQ &amp;&amp; tryAgainQ );
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public void writeOutputLine(String line){ &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; fileOut.println(line);
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; public void closeOutputFile(){
&nbsp; &nbsp; &nbsp; &nbsp; fileOut.close();
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public int nextInt(){
&nbsp; &nbsp; &nbsp; &nbsp; return fileIn.nextInt();
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public String nextString(){
&nbsp; &nbsp; &nbsp; &nbsp; return fileIn.next();
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public boolean hasNextInt(){
&nbsp; &nbsp; &nbsp; &nbsp; if (fileIn.hasNextInt()){
&nbsp; &nbsp; &nbsp; &nbsp; return true;
&nbsp; &nbsp; }
&nbsp; &nbsp; else
&nbsp; &nbsp; return false;
&nbsp; &nbsp; }
&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; public boolean hasNext(){
&nbsp; &nbsp; &nbsp; &nbsp; if(fileIn.hasNext()){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; return false;
&nbsp; &nbsp; }
}