Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,53 +1,16 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.*;
import java.nio.file.*;
public class GlobalReplace
{
public static void main(String[] args)
{
//Enter names of the files here
String[] filename={"foobar0.txt","foobar1.txt","foobar2.txt","foobar3.txt","foobar4.txt"};
//Enter text to replace here
String from="Goodbye London!";
//Enter replacing text here
String to="Hello New York!";
GlobalReplace now=new GlobalReplace();
for(int i=0;i<filename.length;i++)
now.delete(filename[i],from,to);
}
void delete(String filename, String from, String to)
{
try
{
BufferedReader br=new BufferedReader(new FileReader(filename));
//String buffer to store contents of the file
StringBuffer sb=new StringBuffer("");
String line;
while((line=br.readLine())!=null)
{
//If from appears, replace it
if(line.contains(from))
sb.append(line.replace(from, to)+"\n");
else
sb.append(line+"\n");
}
br.close();
FileWriter fw=new FileWriter(new File(filename));
//Write entire string buffer into the file
fw.write(sb.toString());
fw.close();
}
catch (Exception e)
{
System.out.println("Something went horribly wrong: "+e.getMessage());
}
}
public class GloballyReplaceText {
public static void main(String[] args) throws IOException {
for (String fn : new String[]{"test1.txt", "test2.txt"}) {
String s = new String(Files.readAllBytes(Paths.get(fn)));
s = s.replace("Goodbye London!", "Hello New York!");
try (FileWriter fw = new FileWriter(fn)) {
fw.write(s);
}
}
}
}