Skip to content

Instantly share code, notes, and snippets.

@hardikm9850
Created July 1, 2020 10:13
Show Gist options
  • Save hardikm9850/fe08a84b538f40db2242540c6907be44 to your computer and use it in GitHub Desktop.
Save hardikm9850/fe08a84b538f40db2242540c6907be44 to your computer and use it in GitHub Desktop.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class FileCopy
{
public static void main (String[] args) throws java.lang.Exception
{
File file = new File("Path goes here");
File outputFile = null;
try{
outputFile = createFile();
}catch(Exception e){
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
if(st.contains("=")){
sb.append(st);
sb.append("\n");
}
}
try{
writeToFile(sb.toString(),outputFile);
}catch(Exception e){
e.printStackTrace();
}
}
private static File createFile() throws Exception {
File myObj = new File("filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
}
return myObj;
}
private static void writeToFile(String data,File file) throws Exception {
FileWriter myWriter = new FileWriter(file.getName());
myWriter.write(data);
myWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment