Goal: Find out how to write to a file.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Programs that Write to Files /// File Name: Receipt.java /// Date Finished: Mar 02, 2016 import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Receipt { public static void main(String[] args) { Scanner input = new Scanner(System.in); PrintWriter fileOut; double price = 2.791; int numGallons; try{ fileOut = new PrintWriter("receipt.txt"); } catch(IOException e) { System.out.println("Sorry, I can't open the file 'receipt.txt' for editing."); System.out.println("Maybe the file exists and is read-only?"); fileOut = null; System.exit(1); } System.out.println("How many gallons of gas do you want?"); System.out.print("> "); numGallons = input.nextInt(); System.out.println("Thanks for your purchase! Your receipt has been printed to \"receipt.txt\"."); fileOut.println( "+------------------------+" ); fileOut.println( "| |" ); fileOut.println( "| CORNER STORE |" ); fileOut.println( "| |" ); fileOut.println( "| 2016-01-25 04:38PM |" ); fileOut.println( "| |" ); fileOut.println( "| Gallons: " + String.format("%14s", numGallons) + "|" ); fileOut.println( "| Price/gallon: $ " + String.format("%7s", price) + "|" ); fileOut.println( "| |" ); fileOut.println( "| Fuel total: $ " + String.format("%02.02f", price * numGallons) + " |" ); fileOut.println( "| |" ); fileOut.println( "+------------------------+" ); fileOut.close(); } }
Goal: Write to a file in a practical application.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: High Score /// File Name: HighScore.java /// Date Finished: Mar 02, 2016 import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class HighScore { public static void main(String[] args) { Scanner input = new Scanner(System.in); String fileName = "data.txt"; String name; int score; System.out.println("You got a high score!"); System.out.print("What's your name? "); name = input.next(); System.out.print("What's your score? "); score = input.nextInt(); System.out.println("Data saved in \"" + fileName + "\"..."); // Doing this will automatically close the writer try (PrintWriter writer = new PrintWriter(fileName)) { writer.println(name); writer.println(score); } catch (IOException e) { e.printStackTrace(); } } }
Goal: Actually use that file for something.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: PROGRAM_NAME /// File Name: DataFromAFile.java /// Date Finished: Mar 02, 2016 import java.io.File; import java.util.Scanner; public class DataFromAFile { public static void main(String[] args) throws Exception { String name; int a, b, c, sum; System.out.print("Getting name and three numbers from file....."); Scanner fileIn = new Scanner(new File("name-and-numbers.txt")); name = fileIn.nextLine(); a = fileIn.nextInt(); b = fileIn.nextInt(); c = fileIn.nextInt(); fileIn.close(); System.out.println("done."); System.out.println("Your name is: " + name); sum = a + b + c; System.out.println(a + " + " + b + " + " + c + " = " + sum); } }
Goal: Read from a preexisting file.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Simple File Input /// File Name: FileInput.java /// Date Finished: Mar 02, 2016 import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileInput { public static void main(String[] args) { String fileName = "name.txt"; String name = ""; System.out.println("Reading from \"" + fileName + "\"..."); try (Scanner in = new Scanner(new File(fileName))) { name = in.nextLine(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("Your name is " + name); } }
Goal: I think the title explains it all.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Summing Three Numbers /// File Name: ThreeNums.java /// Date Finished: Mar 02, 2016 import java.io.File; import java.io.IOException; import java.util.Scanner; public class ThreeNums { public static void main(String[] args) { int n1 = 0, n2 = 0, n3 = 0; String fileName = "3nums.txt"; System.out.print("Grabbing information from \"" + fileName + "\"... "); try (Scanner in = new Scanner(new File(fileName))) { n1 = in.nextInt(); n2 = in.nextInt(); n3 = in.nextInt(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("DONE"); System.out.println(n1 + " + " + n2 + " + " + n3 + " = " + (n1 + n2 + n3)); } }
Goal: Same as above, but you get to choose the file.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Summing Three Numbers from Any File /// File Name: ThreeNums2.java /// Date Finished: Mar 02, 2016 import java.io.File; import java.io.IOException; import java.util.Scanner; public class ThreeNums2 { public static void main(String[] args) { Scanner userIn = new Scanner(System.in); String choice = ""; String file1 = "3nums1.txt", file2 = "3nums2.txt", file3 = "3nums3.txt"; System.out.println("Choose which file to read numbers from (\"" + file1 + "\", \"" + file2 + "\", \"" + file3 + "\")."); System.out.println("Type \"quit\" to quit."); while (!choice.equals("quit")) { System.out.print("> "); choice = userIn.next(); if (choice.equals(file1) || choice.equals(file2) || choice.equals(file3)) { sum(choice); } else if (!choice.equals("quit")) { System.out.println("Invalid input: " + choice); } } } public static void sum(String fileName) { System.out.print("Reading numbers from \"" + fileName + "\"... "); int n1 = 0, n2 = 0, n3 = 0; try (Scanner in = new Scanner(new File(fileName))) { n1 = in.nextInt(); n2 = in.nextInt(); n3 = in.nextInt(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("DONE"); System.out.println(n1 + " + " + n2 + " + " + n3 + " = " + (n1 + n2 + n3)); } }
Goal: Scan a file and output some cool information about it.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Getting all Data from a File /// File Name: GettingWholeFile.java /// Date Finished: Mar 03, 2016 import java.io.File; import java.util.Scanner; public class GettingWholeFile { public static void main(String[] args) throws Exception { int fourLetter = 0; int caps = 0; String fn1 = "some-words.txt"; String fn2 = "src/fileio/_126/GettingWholeFile.java"; Scanner wordReader = new Scanner(new File(fn1)); while(wordReader.hasNext()) { String w = wordReader.next(); if(w.length() == 4) fourLetter++; } wordReader.close(); Scanner selfInput = new Scanner(new File(fn2)); while(selfInput.hasNext()) { String token = selfInput.next(); if(Character.isUpperCase(token.charAt(0))) caps++; } selfInput.close(); System.out.println( fourLetter + " four-letter words in " + fn1); System.out.println( caps + " words start with capitals in " + fn2); } // How Can Mirrors Be Real If Out Eyes Aren't Real? }
Goal: See the entirety of a file's contents.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Displaying a File /// File Name: DisplayingAFile.java /// Date Finished: Mar 03, 2016 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class DisplayingAFile { public static void main(String[] args) { Scanner userIn = new Scanner(System.in); String choice = ""; while (!choice.equals("!quit")) { System.out.println("Open which file? (type \"!quit\" to quit)"); System.out.print("> "); choice = userIn.next(); if (!choice.equals("!quit")) { printContentsOfFile(choice); } } } public static void printContentsOfFile(String fileName) { try (Scanner in = new Scanner(new File(fileName))) { while (in.hasNext()) { System.out.println(in.nextLine()); } } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } } }
Goal: Same as #125, but there are an unknown amount of numbers per file.
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: Summing Several Numbers from Any File /// File Name: Summing3.java /// Date Finished: Mar 03, 2016 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Summing3 { public static void main(String[] args) { Scanner userIn = new Scanner(System.in); String choice; while (true) { System.out.println("Which file do you want to read numbers from? (\"!quit\" to quit)"); System.out.println("(\"4nums.txt\", \"5nums.txt\", \"6nums.txt\")"); System.out.print("> "); choice = userIn.next(); System.out.println(); if (choice.equals("!quit")) { break; } sumNumbers(choice); } } public static void sumNumbers(String fileName) { try (Scanner in = new Scanner(new File(fileName))) { System.out.println("Reading from " + fileName + "..."); int total = 0; while (in.hasNext()) { int n = in.nextInt(); System.out.print(n + " "); total += n; } System.out.println(); System.out.println("Total is " + total + "\n"); } catch (FileNotFoundException e) { System.out.println("File does not exist: " + fileName); } } }
Goal: Read from the INTERNET!
/// Name: Cornelius Eanes /// Period: 5 /// Program Name: PROGRAM_NAME /// File Name: SimpleWebInput.java /// Date Finished: Mar 03, 2016 import java.net.URL; import java.util.Scanner; public class SimpleWebInput { public static void main(String[] args) throws Exception { URL mURL = new URL("https://gist.githubusercontent.com/whizvox/ac8fd389ea2459924bf9/raw/b5eefcb1fb7ee21240501d2e2aa82c0e5d6cb62c/gistfile1.txt"); Scanner webIn = new Scanner(mURL.openStream()); while (webIn.hasNext()) { System.out.println(webIn.nextLine()); } webIn.close(); } }