Created
November 7, 2011 14:36
-
-
Save jirkapenzes/1345177 to your computer and use it in GitHub Desktop.
Input/output string line from text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.ArrayList; | |
public class FileManager { | |
private static volatile FileManager instance; | |
private static final Object lock = new Object(); | |
private final ArrayList<String> msg; | |
private final String FILE_NAME = "Data.txt"; | |
private FileManager() { | |
msg = new ArrayList<String>(); | |
} | |
public static FileManager Get() { | |
if (instance == null) { | |
synchronized (lock) { | |
instance = new FileManager(); | |
} | |
} | |
return instance; | |
} | |
void addToWrite(String s) { | |
msg.add(s); | |
} | |
public void addToWrite(ArrayList<String> sList) { | |
for (String s : sList) | |
addToWrite(s); | |
} | |
public void save(String filePath) { | |
File file; | |
try { | |
file = new File(filePath); | |
file.createNewFile(); | |
PrintWriter output = new PrintWriter(new FileWriter(file)); | |
for (String s : this.msg) { | |
output.println(s); | |
} | |
output.close(); | |
} catch (IOException e) { | |
System.err.print(e); | |
} | |
} | |
public void save() { | |
save(FILE_NAME); | |
} | |
public ArrayList<String> load(String filePath) { | |
ArrayList<String> stringList = new ArrayList<String>(); | |
FileInputStream stream; | |
try { | |
stream = new FileInputStream(FILE_NAME); | |
BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(stream))); | |
String strLine; | |
while ((strLine = br.readLine()) != null) { | |
stringList.add(strLine); | |
} | |
} catch (IOException e) { | |
return new ArrayList<String>(); | |
} | |
return stringList; | |
} | |
public ArrayList<String> load() { | |
return load(FILE_NAME); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment