Skip to content

Instantly share code, notes, and snippets.

@tonussi
Last active December 19, 2015 01:39
Show Gist options
  • Save tonussi/5877795 to your computer and use it in GitHub Desktop.
Save tonussi/5877795 to your computer and use it in GitHub Desktop.
Method for delete some input string on a input path for a certain file
public class Main {
public static void main(String[] args) {}
public static void add(String fileName, String text) throws FileNotFoundException, IOException {
RandomAccessFile rafile = new RandomAccessFile(new File(fileName), "rw");
rafile.seek(0);
rafile.write(text.getBytes());
rafile.close();
}
public static void append(String fileName, String text) throws FileNotFoundException, IOException {
File f = new File(fileName);
long fileLength = f.length();
RandomAccessFile rafile = new RandomAccessFile(f, "rw");
rafile.seek(fileLength);
rafile.writeBytes(text);
rafile.close();
}
public static void append(String fileName, byte[] bytes) throws FileNotFoundException, IOException {
File f = new File(fileName);
long fileLength = f.length();
RandomAccessFile rafile = new RandomAccessFile(f, "rw");
rafile.seek(fileLength);
rafile.write(bytes);
rafile.close();
}
public static void addRootNode(String path) throws FileNotFoundException, IOException {
//Locate the file
File file = new File(path);
//Create a temporary file (avoid memory call)
File temp = File.createTempFile("file", ".txt", file.getParentFile());
//Determine the charset
String charset = "UTF-8";
//Open file
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
//Open the temp file for writing
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
//Read line by line (not good) generate minimum O(n)
reader.close();
writer.close();
file.delete();
temp.renameTo(file);
}
public static void copyFile(String src, String temp) {
InputStream inStream = null;
OutputStream outStream = null;
try {
File afile = new File(src);
File bfile = new File(temp);
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void removeCertainLineFromFile(String path, String delete) throws FileNotFoundException, IOException {
//Locate the file
File file = new File(path);
//Create a temporary file (avoid memory call)
File temp = File.createTempFile("file", ".txt", file.getParentFile());
//Determine the charset
String charset = "UTF-8";
//Open file
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
//Open the temp file for writing
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
//Read line by line (not good) generate minimum O(n)
for (String line; (line = reader.readLine()) != null;) {
//Determine the string from the file
line = line.replace(delete, "\n");
writer.write(line);
}
reader.close();
writer.close();
file.delete();
temp.renameTo(file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment