import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.FileOutputStream
public class IoPlay {
public static void main(String[] args) throws IOException{
File f = new File("E:\\新建文本文档.txt")
File f1 = new File("D:\\")
IoPlay io = new IoPlay()
io.copy(f,f1)
}
public void copy(File f,File f1) throws IOException{//复制文件的方法!
if(!f1.exists()){
f1.mkdir()
}
if(!f1.exists()){//路径判断,是路径还是单个的文件
File[] cf = f.listFiles()
for(File fn : cf){
if(fn.isFile()){
FileInputStream fis = new FileInputStream(fn)
FileOutputStream fos = new FileOutputStream(f1 + "\\" +fn.getName())
byte[] b = new byte[1024]
int i = fis.read(b)
while(i != -1){
fos.write(b, 0, i)
i = fis.read(b)
}
fis.close()
fos.close()
}else{
File fb = new File(f1 + "\\" + fn.getName())
fb.mkdir()
if(fn.listFiles() != null){//如果有子目录递归复制子目录!
copy(fn,fb)
}
}
}
}else{
FileInputStream fis = new FileInputStream(f)
FileOutputStream fos = new FileOutputStream(f1 + "\\" +f.getName())
byte[] b = new byte[1024]
int i = fis.read(b)
while(i != -1){
fos.write(b, 0, i)
i = fis.read(b)
}
fis.close()
fos.close()
}
}
}
以上是我自己写的,讲一个文件复制到例外一个地方,无论是文件,还是文件夹都可以
基本 *** 作如下:你要补充的是:
1 动态输入内容
2 异常处理
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.util.LinkedList
public class Command {
private java.lang.Process p
private InputStream is
private OutputStream os
private BufferedWriter bw
private BufferedReader br
private ProcessBuilder pb
private InputStream stdErr
public Command() {
}
//获取Process的输入,输出流
public void setCmd(String cmd) {
try {
p = Runtime.getRuntime().exec(cmd)
os = p.getOutputStream()
is = p.getInputStream()
stdErr = p.getErrorStream()
} catch (IOException e) {
System.err.println(e.getMessage())
}
}
//向Process输出命令
public void writeCmd(String cmd) {
try {
bw = new BufferedWriter(new OutputStreamWriter(os))
bw.write(cmd)
bw.newLine()
bw.flush()
bw.close()
} catch (Exception e) {
e.printStackTrace()
}
}
//读出Process执行的结果
public String readCmd() {
StringBuffer sb = new StringBuffer()
br = new BufferedReader(new InputStreamReader(is))
String buffer = null
try {
while ((buffer = br.readLine()) != null) {
sb.append(buffer + "\n")
}
System.out.println(p.waitFor())
} catch (Exception e) {
e.printStackTrace()
}
return sb.toString()
}
//将命令一股脑塞入list中
public LinkedList<String> doCmd(LinkedList<String> lists) {
LinkedList<String> list = new LinkedList<String>()
for (String s : lists) {
writeCmd(s)
list.add(readCmd())
}
return list
}
public static void main(String[] args) {
Command cmd = new Command()
cmd.setCmd("cmd")
cmd.writeCmd("copy d:\\a.txt e:\\ ")
System.out.println(cmd.readCmd())
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)