Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Auxiliar;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.List;
- import java.io.*;
- public class FileHandler {
- private BufferedReader fRead;
- private BufferedWriter fWriter;
- public List<String> readFile(String ficheiro) throws IOException {
- List<String> lines = new ArrayList<String>();
- String line;
- //abrir ficheiro para leitura
- this.abreLeitura(ficheiro);
- //ler linha a linha
- for(int i=0; ;i++) {
- line = readOneLine();
- if(line == null) {
- break;
- }
- lines.add(line);
- continue;
- }
- //fechar ficheiro para leitura
- this.fechaLeitura();
- return lines;
- }
- public void writeFile(Hashtable<String, String> hash, String file) throws IOException {
- abreEscrita(file);
- String kk;
- Enumeration<String> e = hash.keys();
- while(e.hasMoreElements()){
- kk = (String)e.nextElement();
- writeLine(kk+"->"+hash.get(kk));
- }
- fechaEscrita();
- }
- private void abreLeitura(String nomeDoFicheiro) throws IOException {
- File f = new File(nomeDoFicheiro);
- fRead = new BufferedReader(new FileReader(f.getAbsolutePath()));
- }
- private String readOneLine() throws IOException {
- return fRead.readLine();
- }
- private void fechaLeitura() throws IOException {
- fRead.close();
- }
- private void abreEscrita(String nomeDoFicheiro) throws IOException {
- File f = new File(nomeDoFicheiro);
- fWriter = new BufferedWriter(new FileWriter(f.getAbsolutePath()));
- }
- private void writeLine(String str) throws IOException {
- fWriter.write(str+"\n");
- }
- private void fechaEscrita() throws IOException {
- fWriter.close();
- }
- }
Add Comment
Please, Sign In to add comment