java 用IO流拷贝文件后并对文件进行保护

能否用java里面的BufferedInputStream和BufferedOutputStream的方式将word文件拷贝到新磁盘并且加以保护?

保护就是打开word文件后,如果要修改的话,需先输入密码。

手误点了两次。刚试了这样的代码是可以的,但是我又有一个新的问题:如果路径没有确定的呢?

package it.cast_01;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//第三方包
import com.spire.doc.Document;
import com.spire.doc.ProtectionType;

/*
 * 使用字节流获取文件,并将生成的文件保护起来
 */

public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		
		//定义数据源和目标源
		String oldname = "e:\\test.doc";
		String newname = "f:\\ted.doc";
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldname));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newname));
		
		//打印流
		int len = 0;
		byte[] bys = new byte[2048];
		while((len=bis.read(bys))!=-1)
		{
			bos.write(bys, 0, len);
		}
		
		//关闭流
		bis.close();
		bos.close();
		
		//重新获取一次新的文件
		Document doc = new Document(newname);
		//对文件进行保护
		doc.protect(ProtectionType.Allow_Only_Reading, "autosoft");
		//替换原来新的文件
		doc.saveToFile(newname);
		
		doc.close();
		
	}
}

 

刚试了这样的代码是可以的,

package it.cast_01;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//第三方包
import com.spire.doc.Document;
import com.spire.doc.ProtectionType;

/*
 * 使用字节流获取文件,并将生成的文件保护起来
 */

public class CopyFileDemo {
	public static void main(String[] args) throws IOException {
		
		//定义数据源和目标源
		String oldname = "e:\\test.doc";
		String newname = "f:\\ted.doc";
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldname));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newname));
		
		//打印流
		int len = 0;
		byte[] bys = new byte[2048];
		while((len=bis.read(bys))!=-1)
		{
			bos.write(bys, 0, len);
		}
		
		//关闭流
		bis.close();
		bos.close();
		
		//重新获取一次新的文件
		Document doc = new Document(newname);
		//对文件进行保护
		doc.protect(ProtectionType.Allow_Only_Reading, "autosoft");
		//替换原来新的文件
		doc.saveToFile(newname);
		
		doc.close();
		
	}
}

但是我又有一个新的问题:如果路径没有确定的呢?

文件传输最好使用FTP形式传输

你调用wordapplication加密