List arr = new ArrayList();
arr.add("fdsf");
arr.add("asdf");
arr.add("abc");
arr.add("fdac");
arr.add("asdf");
arr.add("abc");
怎么把这里边的“abc”删除?想了半天,我是菜鸟不行不会
arr.add("fdsf");
arr.add("asdf");
arr.add("abc");
arr.add("fdac");
arr.add("asdf");
arr.add("abc");
怎么把这里边的“abc”删除?想了半天,我是菜鸟不行不会
ls.add("abc");
arr.removeAll(ls);
如果做增删除用linklst或map比较好
List arr = new ArrayList();
arr.add("fdsf");
arr.add("asdf");
arr.add("abc");
arr.add("fdac");
arr.add("asdf");
arr.add("abc");
ArrayList a=new ArrayList();//创建一个新集合
a.add("abc");//添加要删除的
al.removeAll(a);//删除所以a里包含的
arr.add("fdsf");
arr.add("asdf");
arr.add("abc");
arr.add("fdac");
arr.add("asdf");
arr.add("abc");
然后怎么删掉里边的“abc”,好像是要用到遍历
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> arr = new ArrayList<String>();
arr.add("fdsf");
arr.add("asdf");
arr.add("abc");
arr.add("fdac");
arr.add("asdf");
arr.add("abc");
for(String str : Test.removeList(arr, "abc")) {
System.out.println(str);
}
}
public static List<String> removeList(List<String> arr, String removeStr) {
List<String> list = new ArrayList<String>();
for(String str : arr) {
if(!str.equals(removeStr) || str != removeStr) {
list.add(str);
}
}
return list;
}
改为.jad
public class CopyFile {
public static void main(String[] args) throws IOException {
File fromPath = new File("D:");
File toPath = new File("c:\\jad");
checkFiles(fromPath, toPath);
} private static void checkFiles(File fromPath, File toPath)
throws IOException {
if (!fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
throw new RuntimeException("文件不存在,请重新输入!");
} else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
copyFiles(fromPath, toPath);
} else {
File[] files = fromPath.listFiles();
for (File file : files) {// 对数组进行遍历
if (file.isDirectory()) {
checkFiles(file, toPath);
} else if (file.getName().endsWith(".java")) {
copyFiles(file, toPath);
}
}
}
}
private static void copyFiles(File cFile, File toDir) throws IOException {//拷贝不了怎么回事
String fileName = cFile.getName();
FileInputStream fis = new FileInputStream(cFile);
FileOutputStream fos = new FileOutputStream(new File(toDir, fileName));
try {
byte[] b = new byte[8192];
int ch = 0;
while (-1 != (ch = fis.read(b))) {
fos.write(b, 0, ch);
}
} finally {
fos.close();
fis.close();
}
}
}
package a;import java.util.ArrayList;
import java.util.List;public class Test1 { /**
* @param args
*/
public static void main(String[] args) {
List arr = new ArrayList();
arr.add("fdsf");
arr.add("asdf");
arr.add("abc");
arr.add("fdac");
arr.add("asdf");
arr.add("abc");
System.out.println("删除前:") ;
for (int i = 0; i< arr.size(); i++){
System.out.print(arr.get(i) + " ") ;
}
System.out.println() ;
for (int i = arr.size()-1 ; i>=0; i--){
if ("abc".equals(arr.get(i))) {
arr.remove(i) ;
}
}
System.out.println("删除后:") ;
for (int i = 0; i< arr.size(); i++){
System.out.print(arr.get(i) + " ") ;
}
System.out.println() ; }}
删除前:
fdsf asdf abc fdac asdf abc
删除后:
fdsf asdf fdac asdf
package a;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class CopyFile {
public static void main(String[] args) throws IOException {
File fromPath = new File("D:");
File toPath = new File("c:\\jad");
checkFiles(fromPath, toPath);
}private static void checkFiles(File fromPath, File toPath)
throws IOException {
if (!fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
throw new RuntimeException("文件不存在,请重新输入!");
} else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
copyFiles(fromPath, toPath);
} else {
File[] files = fromPath.listFiles();
for (File file : files) {// 对数组进行遍历
if (file.isDirectory()) {
checkFiles(file, toPath);
} else if (file.getName().endsWith(".java")) {
copyFiles(file, toPath);
}
}
}
}
private static void copyFiles(File cFile, File toDir) throws IOException {//拷贝不了怎么回事
String fileName = cFile.getName();
FileInputStream fis = null;
FileOutputStream fos = null ;
try {
//放到 try中新建
fis = new FileInputStream(cFile) ;
fos = new FileOutputStream(new File(toDir, fileName));
byte[] b = new byte[8192];
int ch = 0;
while (-1 != (ch = fis.read(b))) {
fos.write(b, 0, ch);
}
} finally {
fos.close();
fis.close();
}
}
}这个代码可以 啊
at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:34)
at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:36)
at com.heima.changjaije.Five.CopyFile.checkFiles(CopyFile.java:36)
at com.heima.changjaije.Five.CopyFile.main(CopyFile.java:23)提示这个错误,大侠指点那.怎么回事?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class CopyFile {
public static void main(String[] args) throws IOException {
File fromPath = new File("D:");
File toPath = new File("c:\\jad");
checkFiles(fromPath, toPath);
} private static void checkFiles(File fromPath, File toPath)
throws IOException {
if (!fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
throw new RuntimeException("文件不存在,请重新输入!");
} else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
copyFiles(fromPath, toPath);
} else {
File[] files = fromPath.listFiles();
for (File file : files) {// 对数组进行遍历
if (file.isDirectory()) {
checkFiles(file, toPath);
} else if (file.getName().endsWith(".java")) {
copyFiles(file, toPath);
}
}
}
} private static void copyFiles(File cFile, File toDir) throws IOException {// 拷贝不了怎么回事
String fileName = cFile.getName();
FileInputStream fis = null;
FileOutputStream fos = null;
if (toDir != null) { //判断一下 ,如果没.java文件,则不做任何操作
try {
fis = new FileInputStream(cFile);
fos = new FileOutputStream(new File(toDir, fileName));
byte[] b = new byte[8192];
int ch = 0;
while (-1 != (ch = fis.read(b))) {
fos.write(b, 0, ch);
}
} finally {
fos.close();
fis.close();
}
}
}
}用这段代码试试
这行
private static void checkFiles(File fromPath, File toPath)
throws IOException {
if (fromPath != null && !fromPath.exists()) {// 测试此抽象路径名表示的文件或目录是否存在
throw new RuntimeException("文件不存在,请重新输入!");
} else if (fromPath.isFile() && fromPath.getName().endsWith(".java")) {
copyFiles(fromPath, toPath);
} else {
File[] files = fromPath.listFiles();
for (File file : files) {// 对数组进行遍历
if (file.isDirectory()) {
checkFiles(file, toPath);
} else if (file.getName().endsWith(".java")) {
copyFiles(file, toPath);
}
}
}
}