在 java控制台实现 类似java Copy srcpath despath这种DOS命令行的文件拷贝功能 文件IO StringBuffer

package day12.IO;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;


public  class MyIoTest {

	/**
	 * 	现在要求完成一个Copy程序,完全模仿DOS中的拷贝命令。
	命令的运行形式,可以通过初始化参数的方式设置两个路径
		· 形式:java Copy 源文件路径 目标文件路径
	本程序完成有两个思路:
		· 思路一:将所有的内容全部读取进来,之后一次性保存
		· 思路二:边读边写
	而且要进行读取的时候还要判断源文件是否存在。
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		createSrcFile();//先创建一个源文件
		JavaCopyMingLing  a  = initial();//读入一个命令
		MyCopy b  =new MyCopy(a); 
		b.copy();
	}

	public static void createSrcFile() throws IOException, Exception
	{
		System.out.println("请输入要创建的源文件的绝对路径");
		StringBuffer sb = new StringBuffer();
		sb.append(new java.util.Scanner(System.in).next());
		sb = changeTo(sb);
		System.out.println(sb);
		Thread.sleep(1000);
		String srcpath = new String(sb);
		File srcFile = new File(srcpath);
		//源文件不存在 就 创建一个新文件
		if(srcFile.exists());
		else
		{
			srcFile.createNewFile();
		}
		System.out.println("请输入你要保存在"+srcpath+"中的内容 输入end并回车表示结束:");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //定义输出流
		BufferedWriter bw = new BufferedWriter(new FileWriter(srcFile));//定义输出流
		String a = null;
//		读入并写文件
		while(!(a = br.readLine()).equals("end"))
		{
			bw.write(a+"\n");
			bw.flush();//边读边写
		}
//		br.close();
//		bw.close();
		System.out.println("文件保存成功");
	}
	public static JavaCopyMingLing initial() throws IOException, InterruptedException
	{
		System.out.println("现在开始执行复制命令\n请输入格式为 java Copy 源文件路径 目标文件路径:");
		String s = new String();
		BufferedReader a = new BufferedReader(new InputStreamReader(System.in));//设定源读入端为consle
		s= a.readLine();//将读入的,命令存入s中
		
		StringBuffer sb = new StringBuffer(s);
		int index = 0 ;
		while((index = sb.indexOf(" "))!=-1)//去除所有空格
			sb.deleteCharAt(index);
		
//		System.out.println(sb);
		Thread.sleep(1000);
		if(sb.indexOf("java")!=0||sb.indexOf("Copy")!=4)  //判断前两个参数是否输入正确
			{
			System.out.println("请输入 java Copy 源文件路径 目标文件路径");
			throw new RuntimeException("命令输入出错");
			}
		//将源目地址取出 并进行转换 再转为String 最后用JavaCopyMingLing 这个类进行封装
		String src = new String(changeTo(new StringBuffer(sb.substring(8,sb.lastIndexOf(":")-1)))); 
		String des = new String(changeTo(new StringBuffer(sb.substring(sb.lastIndexOf(":")-1,sb.length()))));
		if(!new File(src).exists())//判断是否存在源文件
		{
			System.out.println("源文件不存在或目录不正确");
			throw new RuntimeException("命令输入出错");
		}
		//目标文件是默认创建的
//		if(!new File(des).exists())
//		{
//			System.out.println("目标文件不存在或目录不正确");
//			throw new RuntimeException("命令输入出错");
//		}
		return new JavaCopyMingLing(src,des);
	}
	public static StringBuffer changeTo(StringBuffer sb) 
	{
		int index = 0;
		//将windows输入的路径 转化为系统结构路径
		while((index = sb.indexOf("/",index))!=-1)
		{
			sb.insert(index, "/");
//			System.out.println(sb);
			index = index+2;
		}
		return sb;
		}

}

package day12.IO;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class MyCopy {
	JavaCopyMingLing a ;

	public MyCopy(JavaCopyMingLing a) {
		super();
		this.a = a;
	}

	public JavaCopyMingLing getA() {
		return a;
	}

	public void setA(JavaCopyMingLing a) {
		this.a = a;
	}
	public void copy()
	{
		BufferedReader br = null;
		BufferedWriter bw  = null;
		try{
		 br = new BufferedReader(new FileReader(new File(a.getSrc())));
		File des = new File(a.getDes());
		if(!des.exists())
			des.createNewFile();
		 bw = new BufferedWriter(new FileWriter(des));
		String a = null;
		while((a = br.readLine())!=null)
		{
			bw.write(a);
			bw.newLine();
		}
		}
		catch(Exception e )
		{
			e.printStackTrace();
		}
		finally{
			// 偷懒了 应该分别写trycatch的
			try{
				bw.close();
				br.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
	}


	

}
package day12.IO;

public class JavaCopyMingLing
{
	String a = "java";
	String b = "Copy";
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	public String getSrc() {
		return src;
	}
	public void setSrc(String src) {
		this.src = src;
	}
	public String getDes() {
		return des;
	}
	public void setDes(String des) {
		this.des = des;
	}
	String src = null;
	String des = null;
public JavaCopyMingLing(String src, String des) {
		super();
		this.src = src;
		this.des = des;
	}
}









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您可以使用Java自带的Swing框架来实现一个简单的GUI界面,然后使用Java IO库来完成文件拷贝功能。 以下是一个简单的实现示例: ```java import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.*; public class CopyFileGUI { private JFrame frame; private JButton srcButton; private JButton destButton; private JButton copyButton; private JLabel statusLabel; private String srcPath; private String destPath; public CopyFileGUI() { // 创建GUI界面 frame = new JFrame("文件拷贝"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 150); frame.setLayout(new GridLayout(4, 1)); // 创建选择文件按钮 srcButton = new JButton("选择文件"); srcButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION) { srcPath = chooser.getSelectedFile().getAbsolutePath(); System.out.println(srcPath); } } }); frame.add(srcButton); // 创建选择目标文件夹按钮 destButton = new JButton("选择目标文件夹"); destButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int returnVal = chooser.showOpenDialog(frame); if (returnVal == JFileChooser.APPROVE_OPTION) { destPath = chooser.getSelectedFile().getAbsolutePath(); System.out.println(destPath); } } }); frame.add(destButton); // 创建拷贝按钮 copyButton = new JButton("开始拷贝"); copyButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (srcPath == null || srcPath.isEmpty()) { statusLabel.setText("请选择文件!"); return; } if (destPath == null || destPath.isEmpty()) { statusLabel.setText("请选择目标文件夹!"); return; } try { // 拷贝文件 FileInputStream inputStream = new FileInputStream(srcPath); FileOutputStream outputStream = new FileOutputStream(destPath + File.separator + new File(srcPath).getName()); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } inputStream.close(); outputStream.close(); statusLabel.setText("文件拷贝成功!"); } catch (IOException ex) { statusLabel.setText("文件拷贝失败:" + ex.getMessage()); } } }); frame.add(copyButton); // 创建状态标签 statusLabel = new JLabel("", JLabel.CENTER); frame.add(statusLabel); // 显示GUI界面 frame.setVisible(true); } public static void main(String[] args) { new CopyFileGUI(); } } ``` 运以上代码,即可看到一个简单的文件拷贝GUI界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值