-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadWriteFile.java
More file actions
executable file
·44 lines (38 loc) · 996 Bytes
/
ReadWriteFile.java
File metadata and controls
executable file
·44 lines (38 loc) · 996 Bytes
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
/* Librarian Assistant Pro - Version 1.7
* Class: ReadWriteFile
* Duties:
* This class helps us to read from and write to text files
* Date: January 2009
*/
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
class ReadWriteFile
{
public ArrayList<String> readFromFile(File textFile)
throws IOException
//Reads a file line by line and
//puts all the lines in an arraylist of strings
{
ArrayList<String> content = new ArrayList<String>();
Scanner input = null;
input = new Scanner(textFile);
while(input.hasNextLine())
{
content.add(input.nextLine());
}
return content;
}
public void writeToFile(ArrayList<String> text, File textFile)
throws IOException
//Writes the content of a string arrayList to the file
{
PrintWriter writer = null;
writer = new PrintWriter(textFile);
for(int i =0; i < text.size(); i++)
{
writer.println(text.get(i));
writer.close();
}
}
}