Created
March 22, 2016 22:04
-
-
Save AnnaBoro/99167fe226918669e3ed to your computer and use it in GitHub Desktop.
list in 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
package lesson8.simplelist; | |
import java.util.Arrays; | |
public class DemoSimpleList { | |
public static void main (String[] args) { | |
FileSimpleList fileSimpleList = new FileSimpleList(); | |
fileSimpleList.add("0000"); | |
fileSimpleList.add(111); | |
System.out.println(Arrays.toString(fileSimpleList.readFromFile())); | |
fileSimpleList.remove("0000"); | |
System.out.println(fileSimpleList.size()); | |
fileSimpleList.add(555); | |
fileSimpleList.add(7); | |
} | |
} |
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
package lesson8.simplelist; | |
import java.io.*; | |
import java.util.Iterator; | |
public class FileSimpleList implements SimpleList<Object>, Serializable, Iterable { | |
private File file; | |
public FileSimpleList() { | |
file = new File("simplelist.txt"); | |
} | |
@Override | |
public void add(Object object) { | |
writeToFile(object); | |
} | |
@Override | |
public boolean contains(Object object) { | |
String[] arrFromFile = readFromFile(); | |
for (String str : arrFromFile) { | |
if (str.equalsIgnoreCase(object.toString())) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public void remove(Object object) { | |
removeFromFile(object); | |
} | |
@Override | |
public int size() { | |
String[] arrFromFile = readFromFile(); | |
int size = 0; | |
for (String str : arrFromFile) { | |
if (!str.equals("")) { | |
size++; | |
} | |
} | |
return size; | |
} | |
@Override | |
public Iterator iterator() { | |
return new SimpleListIterator(); | |
} | |
public void writeToFile(Object object) { | |
try(FileOutputStream outputStream = new FileOutputStream(file, true); | |
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); | |
BufferedWriter writer = new BufferedWriter(outputStreamWriter); | |
) { | |
String[] arrFromFile = readFromFile(); | |
boolean hasEmpty = false; | |
for (int i = 0; i < arrFromFile.length; i++) { | |
if (arrFromFile[i].equals("")) { | |
arrFromFile[i] = object.toString(); | |
hasEmpty = true; | |
break; | |
} | |
} | |
if (!hasEmpty) { | |
writer.write(object.toString() + "\n"); | |
} | |
else { | |
outputStream.getChannel().truncate(0); | |
for (int i = 0; i < arrFromFile.length; i++) { | |
writer.write(arrFromFile[i] + "\n"); | |
} | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public String[] readFromFile() { | |
StringBuilder builder = new StringBuilder(); | |
try(InputStream inputStream = new FileInputStream(file); | |
InputStreamReader inputStreamReader = new InputStreamReader(inputStream); | |
BufferedReader reader = new BufferedReader(inputStreamReader)) { | |
String str; | |
while ((str = reader.readLine()) != null) { | |
builder = builder.append(str); | |
builder = builder.append("\n\r"); | |
} | |
System.out.println(builder); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return builder.toString().split("\n\r"); | |
} | |
public void removeFromFile(Object object) { | |
try(FileOutputStream outputStream = new FileOutputStream(file, true); | |
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); | |
BufferedWriter writer = new BufferedWriter(outputStreamWriter); | |
) { | |
String[] arrFromFile = readFromFile(); | |
int objPosition = getObjectPosition(object, arrFromFile); | |
System.out.println(objPosition); | |
if (objPosition > -1) { | |
arrFromFile[objPosition] = ""; | |
outputStream.getChannel().truncate(0); | |
for (int i = 0; i < arrFromFile.length; i++) { | |
writer.write(arrFromFile[i] + "\n"); | |
} | |
} | |
else { | |
System.out.println("There isn't such object"); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public int getObjectPosition(Object object, String[] arr) { | |
String[] arrFromFile = arr; | |
for (int i = 0; i < arrFromFile.length; i++) { | |
if (arrFromFile[i].equalsIgnoreCase(object.toString())) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
private class SimpleListIterator implements Iterator{ | |
String[] arrFromFile = readFromFile(); | |
private int curr; | |
public SimpleListIterator() { | |
curr = 0; | |
} | |
@Override | |
public boolean hasNext() { | |
if (arrFromFile[curr + 1] != null) { | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public Object next() { | |
if (curr == 0 && arrFromFile[0] != null) { | |
return arrFromFile[0]; | |
} | |
else if (hasNext()) { | |
curr++; | |
return arrFromFile[curr]; | |
} | |
return null; | |
} | |
@Override | |
public void remove() { | |
} | |
} | |
} |
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
package lesson8.simplelist; | |
import java.util.Iterator; | |
public interface SimpleList<T> { | |
public void add(T object); | |
public boolean contains(T object); | |
public void remove(T object); | |
public int size(); | |
public Iterator<T> iterator(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment