Created
April 16, 2012 13:28
-
-
Save pekkavaa/2398820 to your computer and use it in GitHub Desktop.
This file contains 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
package sanakirja; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Scanner; | |
public class OmaMuistavaSanakirja implements MuistavaSanakirja { | |
private HashMap<String, String> sanat; | |
private String tiedostonimi; | |
public OmaMuistavaSanakirja(String tiedostonimi) { | |
this.sanat = new HashMap<String, String>(); | |
this.tiedostonimi = tiedostonimi; | |
} | |
@Override | |
public void lisaa(String sana, String kaannos) { | |
this.sanat.put(sana, kaannos); | |
} | |
@Override | |
public String kaanna(String kaannos) { | |
if (this.sanat.containsKey(kaannos)) { | |
return this.sanat.get(kaannos); | |
} | |
return null; | |
} | |
@Override | |
public void poista(String sana) { | |
if (this.sanat.containsKey(sana)) { | |
this.sanat.remove(sana); | |
} | |
} | |
// omia testailuja varten | |
public void tulostaTiedosto() throws IOException { | |
File filu = new File(this.tiedostonimi); | |
Scanner lukija = new Scanner(filu, "UTF-8"); | |
while (lukija.hasNextLine()) { | |
String rivi = lukija.nextLine(); | |
System.out.println(rivi); | |
} | |
lukija.close(); | |
} | |
@Override | |
public void lataa() throws IOException { | |
File filu = new File(this.tiedostonimi); | |
String rivi; | |
if (filu.isFile()) { | |
Scanner lukija = new Scanner(filu); | |
this.sanat.clear(); | |
while (lukija.hasNextLine()) { | |
rivi = lukija.nextLine(); | |
String[] osat = rivi.split(" "); | |
this.sanat.put(osat[1], osat[0]); | |
} | |
lukija.close(); | |
} | |
} | |
@Override | |
public void talleta() throws IOException { | |
int r=0; | |
FileWriter kirjuri = new FileWriter(this.tiedostonimi); | |
for (String k : this.sanat.keySet()) { | |
kirjuri.write(sanat.get(k)); | |
kirjuri.write(" "); | |
kirjuri.write(k); | |
kirjuri.write("\n"); | |
r++; | |
} | |
kirjuri.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment