Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java - Exception Handling and File Objects accounts.txt file contains the follow

ID: 3688871 • Letter: J

Question

Java - Exception Handling and File Objects

accounts.txt file contains the following information:

Jones   369218658389641          
Smith   6011781008881301          
Wayne   5551066751345482          
Wines   4809134775860432          
Biggie   9925689541232325          
Luke   7586425896325410          
Brandy   4388576018410707          
Ryan   2458912425860439          

1. (Write /read data) Write a program that reads from the file, accounts.txt and then writes the contents into a new file. myFile.txt

2. (Write /read data) Write a program that creates file called testFile.txt, if it does not exist. Write 100 random generated integers and write them to the file, each on a new line as shown below

34

23

23

Explanation / Answer

//Program 1
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class ReadWriteToFile {

   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;

       try {

           File inputFile = new File("accounts.txt");
           scanner = new Scanner(inputFile);

           File outputFile = new File("myFile.txt");
           if (!outputFile.exists()) {
               outputFile.createNewFile();
           }
           FileWriter fw = new FileWriter(outputFile.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);

           while (scanner.hasNext()) {
               String line = scanner.nextLine();
               bw.write(line + " ");

           }
           bw.close();

           System.out.println("Data written successfully to myFile.txt file");

       } catch (Exception e) {
           // TODO: handle exception
       } finally {

           scanner.close();

       }

   }
}

accounts.txt
Jones 369218658389641
Smith 6011781008881301
Wayne 5551066751345482
Wines 4809134775860432
Biggie 9925689541232325
Luke 7586425896325410
Brandy 4388576018410707
Ryan 2458912425860439

myFile.txt
Jones 369218658389641
Smith 6011781008881301
Wayne 5551066751345482
Wines 4809134775860432
Biggie 9925689541232325
Luke 7586425896325410
Brandy 4388576018410707
Ryan 2458912425860439

OUTPUT:
Data written successfully to myFile.txt file


//Program 2
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;

/**
* @author Srinivas Palli
*
*/
public class WriteRandomToFile {

   /**
   * @param args
   */
   public static void main(String[] args) {

       try {
           File outputFile = new File("testFile.txt");
           if (!outputFile.exists()) {
               outputFile.createNewFile();
           }
           FileWriter fw = new FileWriter(outputFile.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);

           Random random = new Random();

           for (int i = 0; i < 100; i++) {
               int randomNum = random.nextInt();
               bw.write(randomNum + " ");
           }
           bw.close();
       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}

testFile.txt
-1972799190
-1070361054
1971148758
-2038602687
1703331881
413307399
1162415257
1730499771
-1817972303
2113443800
-908965790
1598770638
762124311
964728689
-995357633
-20070293
2020977961
-753940820
-201622933
-1682584653
-35353302
-1203586537
161288948
549283532
1788364314
-291056409
-1907452258
1244517367
-1011740870
-218741715
2111419399
1355395298
1549415630
658241863
-94772469
-103335878
-1384580060
1964529560
1366954601
-2020308860
1380664277
397838715
1294797734
-1884748755
493511754
361607284
606974257
449060575
1565725825
2080924840
1942125255
46869525
-437299439
1373648782
1041355800
-2130902287
2053354347
-945672729
-1660206677
-1937011892
1872772962
-72588053
2076405758
-1449621489
1228553452
-1354475836
1700761840
1233400512
521883689
1080586815
596552547
-447644577
-2015391323
-956892736
894812445
-1563621517
-779353895
787438967
-203559185
174141279
-2066015497
-466451247
-1002312936
391295225
-1896506905
561625904
-582318614
-1815689124
1179230092
730018781
-1260384082
648861693
-1584213685
-571582147
82890436
-122491652
-129357876
-1875813566
-789916402
806204604

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote