Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created September 1, 2016 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/9d20738ef12ef5220f8aac0567ede123 to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/9d20738ef12ef5220f8aac0567ede123 to your computer and use it in GitHub Desktop.
IO
package ua.artcode.week4.day2;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.DuplicateFormatFlagsException;
import java.util.List;
/**
* Created by alexnagorniy on 28.08.16.
*/
public class NagorniyIO implements IBash {
// show file content
@Override
public String less(String path) throws FileNotFoundException { //todo check the file for existence, and if it does not exist, throw the exception to the top
String result = ""; //todo use StringBuilder
String temp = "";
try (BufferedReader reader = new BufferedReader(new FileReader(path))){
while ((temp = reader.readLine()) != null) {
result += temp + "\n";
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
public void writeInto(String path, String content) throws FileNotFoundException { //todo check the file for existence, and if it does not exist, throw the exception to the top
try (FileWriter writer = new FileWriter(path)) {//todo use BufferedWriter too
writer.write(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void appendTo(String path, String content) throws FileNotFoundException {
try (FileWriter writer = new FileWriter(path, true)) { //todo use BufferedWriter too
writer.append(content);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public List<String> ls(String currentDirPath) {
return Arrays.asList(new File(currentDirPath).list()); //todo check null, because list() can return null
}
// touch == create new file
@Override
public boolean touch(String path) {
File file = new File(path);
if (!file.exists()) {
try {
return file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("False touch: " + path + ", file already exists");
return false;
}
return false;
}
@Override
public boolean delete(String path) {
File file = new File(path);
if (file.exists()) {
return file.delete();
} else {
System.out.println("False delete: " + path + ", not found");
return false;
}
}
@Override
public boolean copy(String src, String dest) {
File source = new File(src);
File destination = new File(dest);
try {
Files.copy(source.toPath(), destination.toPath());
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
public List<String> grep(String src, String keyWord) {
File[] files = new File(src).listFiles();
ArrayList<String> result = new ArrayList<>(); //todo List<String> result = new ArrayList<>();
String temp = "";
try (BufferedReader reader = new BufferedReader(new FileReader(src))){
for (File file: files) {//todo reader must be by file, not src, file is not used now
while ((temp = reader.readLine()) != null) {
if (temp.contains(keyWord)){
result.add(temp);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
public Object clone(Object obj) {
try (ByteArrayOutputStream outByteArr = new ByteArrayOutputStream(); //todo use too BufferedOutputStream/BufferedInnerStream
ObjectOutputStream out = new ObjectOutputStream(outByteArr);
ObjectInputStream input =
new ObjectInputStream(
new ByteArrayInputStream(outByteArr.toByteArray())) //todo test fails, because you must initialize after writing in outByteArr
){
out.writeObject(obj);
return input.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
public byte[] toByteArr(Object obj) {
try (ByteArrayOutputStream outByteArr = new ByteArrayOutputStream(); //todo use too BufferedOutputStream
ObjectOutputStream out = new ObjectOutputStream(outByteArr)){
out.writeObject(obj);
return outByteArr.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return new byte[0];
}
@Override
public Object fromByteArr(byte[] arr) {
try (ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(arr))) { //todo use too BufferedInputStream
return input.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object cloneDeep(Object obj) {
return null;
}
@Override
public void saveObjToFile(Object obj, String filePath) {
}
@Override
public void downloadFile(String url, String localPathName) {
}
@Override
public List<String> find(File dir, String keyWord) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment