-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.java
More file actions
94 lines (76 loc) · 2.67 KB
/
Copy pathFileIO.java
File metadata and controls
94 lines (76 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* This code is my own work. I did not get any help from any online source
such as chegg.com; from a classmate, or any other person other than the instructor
or TA for this course. I understand that getting outside help from this course
other than from the instructor or TA will result in a grade of 0 in this
assignment and other disciplinary actions for academic dishonesty.
# Name : Cristian Z
# Class: CSET 1200
# Instructor: Dr. Jared Oluoch
# Programming Assignment: 11
# Problem: 2
# Date: 11/19/21
# Summary: write and read from file like we are in 1st grade, but doing it with java now
# Problem 2 (5 Points)
Write a Java program that reads from a text file and prints out the output.
The text file should be named your last name.txt for example doe.txt.
The text file should contain the following information:
Course, Number, Instructor, Credit hours.
So you could have something like:
CSET, 1200, Oluoch, 3
ENGT, 2000, Alaraje, 1
Make sure you have at least 5 rows and you must have commas seperating
each column.
Read from the text file and print out all contents.
https://youtu.be/XlUGyEj-iVM
*/
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.Scanner;
import java.io.*;
import java.util.*; //We really don't care anymore look for all neccessary packages and WORK!!
public class FileIO{
public static void main(String[] args) throws java.io.IOException {
//call my file "file" for short while making an instance
java.io.File file = new java.io.File("Zaragoza.txt");
//check if it exists first to cut the work
if(file.exists()==true)
{
System.out.println("File " + file + " already exists.");
//Scan file while it has lines to read and output
Scanner read = new Scanner(file);
while (read.hasNextLine())
{
String data = read.nextLine();
System.out.println(data);
}
System.exit(0);
}
else
{
//try making the file just in case
try(
//create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
)
{
//write formated output to the file
output.println("CSET, 1200, Oluoch, 3 ");
output.println("CSET, 2230, Langenderfer, 4 ");
output.println("MATH, 2450, Robinson, 4 ");
output.println("CSET, 2200, Brahaney, 4 ");
output.println("CSET, 2200, Brahaney, 0 ");
//Close the file
output.close();
}
//read the file now
System.out.println("File " + file + " was made.");
Scanner read = new Scanner(file);
while (read.hasNextLine())
{
String data = read.nextLine();
System.out.println(data);
}
}
}
}