我写了一个小程序有两个方法,一个是Change()用来把一个文件夹里的图片用BufferedInputStream读出,然后再用BufferedWriter写成文本形式放入另一个文件夹.另一个方法是UnChange()用来把文本形式的文件用BufferedReader读出,再用BufferedOutputStream写成图片格式存到另一个文件夹.
编译通过后,调用Change()可以正常运行.但是一调用UnChange()就提示下面的错:
Exception in thread "main" java.lang.NoSuchMethodError
at MyPic.UnChange(MyPic.java:65)
at MyPic.main(MyPic.java:95)
改来改去也不行,现在把代码帖出来,请大家帮我看看是那里出问题了.谢谢了.
import java.io.*;
public class MyPic {
private File source;
private File targer;
private int count;
private int jpgNum;
MyPic(String document,String operat) throws IOException{
String temp="";
source=new File(document);
if(operat.equals("1")){
temp=source.getParent()+"\\code";
}
if(operat.equals("2")){
temp=source.getParent()+"\\jpg";
}
targer=new File(temp);
if(!targer.exists()){
targer.mkdir();
}
}
public void Change() throws IOException{
File[] pics=source.listFiles();
String path=targer.getPath()+"\\";
for(int i=0;i<pics.length;i++){
if(!pics[i].isFile()){
continue;
}
jpgNum++;
String name=path+pics[i].getName()+".code";
BufferedInputStream bufr=new BufferedInputStream(new FileInputStream(pics[i]));
BufferedWriter bufw=new BufferedWriter(new FileWriter(name));
int temp;
while((temp=bufr.read())!=-1){
count++;
String key=Integer.toString(temp);
if(count%100==0){
bufw.write(key+"\n");
}else{
bufw.write(key+",");
}
}
bufr.close();
bufw.close();
}
System.out.println("Change success!");
System.out.println("Change "+jpgNum+" files!");
}
public void UnChange() throws IOException{
File[] codes=source.listFiles();
String path=targer.getPath()+"\\";
for(int i=0;i<codes.length;i++){
if(!codes[i].isFile()){
continue;
}
jpgNum++;
String name=path+codes[i].getName();
name=name.substring(0, name.length()-5);
BufferedReader br=new BufferedReader(new FileReader(codes[i]));
BufferedOutputStream bufw=new BufferedOutputStream(new FileOutputStream(name));
String s="";
while((s=br.readLine())!=null){
String[] s1=s.split(",");
for(int j=0;j<s1.length;j++){
int temp=Integer.parseInt(s1[i]);
bufw.write(temp);
}
}
br.close();
bufw.close();
}
System.out.println("UnChange success!");
System.out.println("UnChange "+jpgNum+" files!");
} public static void main(String[] args) { String document="";
if(args.length<2){
System.out.println("Please spesify a document,\nlike this:MyPic d:\\PicDocument code");
System.exit(1);
}
else{
document=args[0];
}
try{
MyPic myPic=new MyPic(document,args[1]);
if(args[1].equals("1")){
myPic.Change();
}
else if(args[1].equals("2")){
myPic.UnChange();
}
else
System.out.println("Error:1 or 2");
}catch (IOException e){
System.out.println(e);
}
}
}
编译通过后,调用Change()可以正常运行.但是一调用UnChange()就提示下面的错:
Exception in thread "main" java.lang.NoSuchMethodError
at MyPic.UnChange(MyPic.java:65)
at MyPic.main(MyPic.java:95)
改来改去也不行,现在把代码帖出来,请大家帮我看看是那里出问题了.谢谢了.
import java.io.*;
public class MyPic {
private File source;
private File targer;
private int count;
private int jpgNum;
MyPic(String document,String operat) throws IOException{
String temp="";
source=new File(document);
if(operat.equals("1")){
temp=source.getParent()+"\\code";
}
if(operat.equals("2")){
temp=source.getParent()+"\\jpg";
}
targer=new File(temp);
if(!targer.exists()){
targer.mkdir();
}
}
public void Change() throws IOException{
File[] pics=source.listFiles();
String path=targer.getPath()+"\\";
for(int i=0;i<pics.length;i++){
if(!pics[i].isFile()){
continue;
}
jpgNum++;
String name=path+pics[i].getName()+".code";
BufferedInputStream bufr=new BufferedInputStream(new FileInputStream(pics[i]));
BufferedWriter bufw=new BufferedWriter(new FileWriter(name));
int temp;
while((temp=bufr.read())!=-1){
count++;
String key=Integer.toString(temp);
if(count%100==0){
bufw.write(key+"\n");
}else{
bufw.write(key+",");
}
}
bufr.close();
bufw.close();
}
System.out.println("Change success!");
System.out.println("Change "+jpgNum+" files!");
}
public void UnChange() throws IOException{
File[] codes=source.listFiles();
String path=targer.getPath()+"\\";
for(int i=0;i<codes.length;i++){
if(!codes[i].isFile()){
continue;
}
jpgNum++;
String name=path+codes[i].getName();
name=name.substring(0, name.length()-5);
BufferedReader br=new BufferedReader(new FileReader(codes[i]));
BufferedOutputStream bufw=new BufferedOutputStream(new FileOutputStream(name));
String s="";
while((s=br.readLine())!=null){
String[] s1=s.split(",");
for(int j=0;j<s1.length;j++){
int temp=Integer.parseInt(s1[i]);
bufw.write(temp);
}
}
br.close();
bufw.close();
}
System.out.println("UnChange success!");
System.out.println("UnChange "+jpgNum+" files!");
} public static void main(String[] args) { String document="";
if(args.length<2){
System.out.println("Please spesify a document,\nlike this:MyPic d:\\PicDocument code");
System.exit(1);
}
else{
document=args[0];
}
try{
MyPic myPic=new MyPic(document,args[1]);
if(args[1].equals("1")){
myPic.Change();
}
else if(args[1].equals("2")){
myPic.UnChange();
}
else
System.out.println("Error:1 or 2");
}catch (IOException e){
System.out.println(e);
}
}
}
while((s=br.readLine())!=null){
String[] s1=s.split(",");
import java.io.*;
public class MyPic {
private File source;
private File targer;
private int count;
private int jpgNum;
MyPic(String document,String operat) throws IOException{
String temp="";
source=new File(document);
if(operat.equals("1")){
temp=source.getParent()+"\\code";
}
if(operat.equals("2")){
temp=source.getParent()+"\\jpg";
}
targer=new File(temp);
if(!targer.exists()){
targer.mkdir();
}
}
public void Change() throws IOException{
File[] pics=source.listFiles();
String path=targer.getPath()+"\\";
for(int i=0;i<pics.length;i++){
if(!pics[i].isFile()){
continue;
}
jpgNum++;
String name=path+pics[i].getName()+".code";
BufferedInputStream bufr=new BufferedInputStream(new FileInputStream(pics[i]));
BufferedWriter bufw=new BufferedWriter(new FileWriter(name));
int temp;
while((temp=bufr.read())!=-1){
count++;
String key=Integer.toString(temp);
if(count%100==0){
bufw.write(key+"\n");
}else{
bufw.write(key+",");
}
}
bufr.close();
bufw.close();
}
System.out.println("Change success!");
System.out.println("Change "+jpgNum+" files!");
}
public void UnChange() throws IOException{
File[] codes=source.listFiles();
String path=targer.getPath()+"\\";
for(int i=0;i<codes.length;i++){
if(!codes[i].isFile()){
continue;
}
jpgNum++;
String name=path+codes[i].getName();
name=name.substring(0, name.length()-5);
//****************************************************************//
// name=name.substring(0, name.lastIndexOf(".")); //
//*****************************************************************//
BufferedReader br=new BufferedReader(new FileReader(codes[i]));
BufferedOutputStream bufw=new BufferedOutputStream(new FileOutputStream(name));
String s="";
while((s=br.readLine())!=null){
String[] s1=s.split(",");
for(int j=0;j<s1.length;j++){
// int temp=Integer.parseInt(s1[j]);
// bufw.write(temp);
//*****************************************************************// int temp=s1[j].length();
byte [] b=new byte[temp]; //*****************************************************************//
bufw.write(b);
}
}
br.close();
bufw.close();
}
System.out.println("UnChange success!");
System.out.println("UnChange "+jpgNum+" files!");
}
public static void main(String[] args) {
args=new String []{"D:\\picture\\新建文件夹 (3)","2"};
String document="";
if(args.length<2){
System.out.println("Please spesify a document,\nlike this:MyPic d:\\PicDocument code");
System.exit(1);
} else{
document=args[0];
}
try{
MyPic myPic=new MyPic(document,args[1]);
if(args[1].equals("1")){
myPic.Change();
} else if(args[1].equals("2")){
myPic.UnChange();
} else
System.out.println("Error:1 or 2");
}catch (IOException e){
System.out.println(e);
}
}
}
不知道你是不是要实现这个效果?
这是我唯一想到的原因!你的JDK版本很高,可惜你忘记更新JRE了,而且我猜你是在命令行运行的, Eclipse里面不会出现类似的问题!
早上在Eclipse里更新了JRE,然后在命令行里运行还是不行,后来我就直接在Eclipse里运行,结果成功.