关于IO流问题关于文件复制的(初学) 我想把c:\file文件夹里后缀名是JPG或者HTML或其他(自己定)的文件复制到d:\file里面,应该怎样做?各位大侠最好给些学习建议~~迷茫~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 注:使用了Apache commons io包(http://commons.apache.org/io/),这种基础操作可以自己写,但是用别人类库更方便。import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.apache.commons.io.filefilter.FileFilterUtils;public class Test { public static void main(String[] args) throws IOException { FileUtils.copyDirectory(new File("c:\\file"), new File("d:\\file"), FileFilterUtils.suffixFileFilter(".jpg")); }} public static void main(String[] args) throws IOException{ File src = new File("c:\\file");//原目录 File tar = new File("d:\\file");//目标目录 tar.mkdirs();//生成目标目录 InputStream fileInput = null; OutputStream fileOutput = null; byte[] buffer = new byte[1024]; //遍历原目录中扩展名为jpg或html的文件 for(File f:src.listFiles(new FilenameFilter(){ public boolean accept(File dir, String name) { return name.endsWith("jpg")||name.endsWith("html"); }})){ fileInput = new FileInputStream(f); fileOutput = new FileOutputStream(tar.getPath()+File.separator+f.getName()); while(fileInput.read(buffer)!= -1) { fileOutput.write(buffer); } } fileInput.close(); fileOutput.close(); } package CSDNTest;import java.io.*;import java.net.URL;public class Download { private static void copyFile(File input, File output) throws IOException { InputStream in = input.toURL().openStream(); try { OutputStream out = new FileOutputStream(output); try { copy(in, out); } finally { out.close(); } } finally { in.close(); } } private static void copy(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; while (true) { int readCount = in.read(buffer); if (readCount == -1) { break; } out.write(buffer, 0, readCount); } } public static void main(String[] args) { try { //URL url = new URL("http://sohu.com"); File input = new File("D:\\a\\update.exe"); File output = new File("D:\\a\\update1.exe"); copyFile(input, output); } catch (IOException e) { e.printStackTrace(); } }}单个复制 没有考虑file下是目录的情况,加上去就完整了 import java.io.*;public class TestFileOutputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("c:\file"); out = new FileOutputStream("d:\file"); while((b=in.read())!=-1){ out.write(b); } in.close(); out.close(); } catch (FileNotFoundException e2) { System.out.println("找不到指定文件"); System.exit(-1); } catch (IOException e1) { System.out.println("文件复制错误"); System.exit(-1); } System.out.println("文件已复制"); }}希望对你有用!我也是初学JAVA。 /*** 读出1.txt中的内容,写入2.txt中**/import java.io.*;public class ReadWriteFile{public static void main(String[] args){try{File read = new File("c:\\1.txt");File write = new File("c:\\2.txt"); BufferedReader br = new BufferedReader(new FileReader(read));BufferedWriter bw = new BufferedWriter(new FileWriter(write));String temp = null;temp = br.readLine();while(temp != null){//写文件bw.write(temp + "\n"); //只适用Windows系统//继续读文件temp = br.readLine();}bw.close();br.close();}catch(FileNotFoundException e){ //文件未找到System.out.println (e);}catch(IOException e){System.out.println (e);}}} Java IO 多种方式读文件一、多种方式读文件内容。1、按字节读取文件内容2、按字符读取文件内容3、按行读取文件内容4、随机读取文件内容http://hi.baidu.com/binky2008/blog/item/9c072744a522cd4f500ffe77.html 快成了找错误了.还有流关闭也有问题.如果不存在jpg或html文件的话,那流就为空。会空指针...我觉得没问题的就那个public static void main.... 所以还是用类库的好,方便实用,而且能满足各种需求。我上面写的仅是其中一个方法,还有很多有用的方法。再复制文件的时候要考虑空指针、路径不存在、文件不存在、覆盖已有文件、确保流关闭、合理实用buffer等一系列问题,否则只能是实验性质的自己写着玩玩的系统。 package filecopy;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.LinkedList;public class File_copy implements FileFilter{ private String savePlace; private String searchPlace; private String type=".jpg"; private LinkedList<String> list; private ArrayList<File> fileList; private boolean targ; /** * @param args the command line arguments */ public void searchFile(String searchPlace){ list=new LinkedList(); fileList=new ArrayList(); list.offer(searchPlace); while(list.peek()!=null){ File file=new File(list.pop()); File[] files=file.listFiles(this); int i=0; for(File f;i<files.length;){f=files[i]; i++; if(f.isDirectory()) list.add(f.getPath()); else fileList.add(f); } } } public void copyFile(String savePlace) throws FileNotFoundException, IOException{ byte[] buff=new byte[1024]; for(int i=0;i<fileList.size();i++){ FileInputStream fis=new FileInputStream(fileList.get(i)); FileOutputStream fos=new FileOutputStream(savePlace+"\\"+fileList.get(i).getName()); int length=fis.read(buff); while(length!=-1){ fos.write(buff,0,length); length=fis.read(buff); } fis.close(); fos.close(); } } public static void main(String[] args) throws FileNotFoundException, IOException { // TODO code application logic here long time=System.currentTimeMillis(); File_copy fc=new File_copy(); fc.searchFile("C:\\Documents and Settings\\rgbhsodiy\\桌面\\123"); fc.copyFile("F:\\110"); System.out.println("There is "+fc.fileList.size()+" files!"); System.out.println("There is cost "+(System.currentTimeMillis()-time)); } public boolean accept(File pathname) { if(pathname.isDirectory()) return true; else if(pathname.isFile()&&pathname.getName().endsWith(type)) return true; else return false; }} Swing中如何选择表格中指定的行? 如何通过鼠标事件绘制图形??????? 一个关于List的问题 求助:JAVA怎样生成EXE文件? 数据库连接问题 j2ee,j2me有什么区别??? 哦~这一次四级应该是过了~接着散 怎么用JBuild编写java console程序 高手请进来看看! 关于静态方法引用的疑问 无法回滚的问题 关于扫雷游戏
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;public class Test { public static void main(String[] args) throws IOException {
FileUtils.copyDirectory(new File("c:\\file"), new File("d:\\file"), FileFilterUtils.suffixFileFilter(".jpg"));
}
}
public static void main(String[] args) throws IOException{
File src = new File("c:\\file");//原目录
File tar = new File("d:\\file");//目标目录
tar.mkdirs();//生成目标目录
InputStream fileInput = null;
OutputStream fileOutput = null;
byte[] buffer = new byte[1024];
//遍历原目录中扩展名为jpg或html的文件
for(File f:src.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.endsWith("jpg")||name.endsWith("html");
}})){
fileInput = new FileInputStream(f);
fileOutput = new FileOutputStream(tar.getPath()+File.separator+f.getName());
while(fileInput.read(buffer)!= -1) {
fileOutput.write(buffer);
}
}
fileInput.close();
fileOutput.close();
}
package CSDNTest;import java.io.*;
import java.net.URL;public class Download {
private static void copyFile(File input, File output) throws IOException {
InputStream in = input.toURL().openStream();
try {
OutputStream out = new FileOutputStream(output);
try {
copy(in, out);
} finally {
out.close();
}
} finally {
in.close();
}
} private static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
while (true) {
int readCount = in.read(buffer);
if (readCount == -1) {
break;
}
out.write(buffer, 0, readCount);
}
} public static void main(String[] args) {
try {
//URL url = new URL("http://sohu.com");
File input = new File("D:\\a\\update.exe");
File output = new File("D:\\a\\update1.exe");
copyFile(input, output);
} catch (IOException e) {
e.printStackTrace();
}
}
}单个复制
public class TestFileOutputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("c:\file");
out = new FileOutputStream("d:\file");
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件"); System.exit(-1);
} catch (IOException e1) {
System.out.println("文件复制错误"); System.exit(-1);
}
System.out.println("文件已复制");
}
}
希望对你有用!我也是初学JAVA。
* 读出1.txt中的内容,写入2.txt中
*
*/import java.io.*;public class ReadWriteFile{
public static void main(String[] args){
try{File read = new File("c:\\1.txt");
File write = new File("c:\\2.txt"); BufferedReader br = new BufferedReader(
new FileReader(read));
BufferedWriter bw = new BufferedWriter(
new FileWriter(write));
String temp = null;
temp = br.readLine();
while(temp != null){
//写文件
bw.write(temp + "\n"); //只适用Windows系统
//继续读文件
temp = br.readLine();
}bw.close();
br.close();}catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
}
} Java IO 多种方式读文件一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
http://hi.baidu.com/binky2008/blog/item/9c072744a522cd4f500ffe77.html
还有流关闭也有问题.如果不存在jpg或html文件的话,那流就为空。会空指针...
我觉得没问题的就那个public static void main....
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
public class File_copy implements FileFilter{
private String savePlace;
private String searchPlace;
private String type=".jpg";
private LinkedList<String> list;
private ArrayList<File> fileList;
private boolean targ;
/**
* @param args the command line arguments
*/
public void searchFile(String searchPlace){
list=new LinkedList();
fileList=new ArrayList();
list.offer(searchPlace);
while(list.peek()!=null){
File file=new File(list.pop());
File[] files=file.listFiles(this);
int i=0;
for(File f;i<files.length;){f=files[i]; i++;
if(f.isDirectory()) list.add(f.getPath());
else fileList.add(f);
}
}
} public void copyFile(String savePlace) throws FileNotFoundException, IOException{
byte[] buff=new byte[1024];
for(int i=0;i<fileList.size();i++){
FileInputStream fis=new FileInputStream(fileList.get(i));
FileOutputStream fos=new FileOutputStream(savePlace+"\\"+fileList.get(i).getName());
int length=fis.read(buff);
while(length!=-1){
fos.write(buff,0,length);
length=fis.read(buff);
}
fis.close();
fos.close();
}
} public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
long time=System.currentTimeMillis();
File_copy fc=new File_copy();
fc.searchFile("C:\\Documents and Settings\\rgbhsodiy\\桌面\\123");
fc.copyFile("F:\\110");
System.out.println("There is "+fc.fileList.size()+" files!");
System.out.println("There is cost "+(System.currentTimeMillis()-time));
} public boolean accept(File pathname) {
if(pathname.isDirectory()) return true;
else if(pathname.isFile()&&pathname.getName().endsWith(type)) return true;
else return false;
}
}