I would like to replace data in a file with some new data and rename the file with a different name in java. I have the old file saved in the same class directory as the code to carry out the changes is saved. The command that I use in the command prompt is as below.
java ReplacingText oldfile newfile oldstring newstring
I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: ReplacingText (wrong name: replacingtext/ReplacingText)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Could someone shed some light on it.
package replacingtext;
import java.io.*;
import java.util.*;
public class ReplacingText
{
public static void main(String[] args) throws Exception
{
if (args.length !=4)
{
System.out.println(
"Usage: java ReplaceText sourceFile targetFile oldStr newStr");
System.exit(0);
}
File sourceFile = new File(args[0]);
if(!sourceFile.exists())
{
System.out.println("Source file " + args[0]+" does not exist");
System.exit(0);
}
File targetFile = new File(args[1]);
if (targetFile.exists())
{
System.out.println("Target File " + args[1] + "already exist");
System.exit(0);
}
Scanner input = new Scanner(sourceFile);
PrintWriter output2 = new PrintWriter(targetFile);
while (input.hasNext())
{
String s1=input.nextLine();
String s2=s1.replaceAll(args[2],args[3]);
output2.println(s2);
}
input.close();
output2.close();
}
}
i found it on the net. first, i make a direcoty,name as "replacingtext" ,the package name. then, i move the complied class "ReplacingText.class" into it. last, i run "java replacingtext.ReplacingText "c:/s.txt" "c:/t.txt" haha yes" in "replacingtext" parent directory.
bingo...it works..
but.. i don't konw why.. perhaps the classLoader find the class by the relative path ,not just the class name...
first,i tested the method like this.
public static void main(String[] args) throws Exception {
String sourceFile = "c://s.txt";
String targetFile = "c://t.txt";
String oldS = "haha";
String newS = "yes";
test(new String[]{sourceFile,targetFile,oldS,newS});
// test(args);
}
private static void test(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
if (args.length != 4) {
System.out
.println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
System.exit(0);
}
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " does not exist");
System.exit(0);
}
File targetFile = new File(args[1]);
if (targetFile.exists()) {
System.out.println("Target File " + args[1] + " already exist");
System.exit(0);
}
Scanner input = new Scanner(sourceFile);
PrintWriter output2 = new PrintWriter(targetFile);
while (input.hasNext()) {
String s1 = input.nextLine();
String s2 = s1.replaceAll(args[2], args[3]);
output2.println(s2);
}
input.close();
output2.close();
}
it works.
then, i tested based on the method above like this.
public static void main(String[] args) throws Exception {
test(args);
}
java ReplacingText c://s.txt c://t.txt haha yes
it worked ,too..
ps:cause i didn't config the JAVA_HOME,so I run it in my jdk/bin directory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.