在D盘中创建文件test.txt,文件中内容为:hello Java,然后利用流把该文件拷贝到 50
要一个完整的运行程序~~~跪求在D盘中创建文件test.txt,文件中内容为:helloJava,然后利用流把该文件拷贝到E盘根目录中...
要一个完整的运行程序~~~跪求
在D盘中创建文件test.txt,文件中内容为:hello Java,然后利用流把该文件拷贝到E盘根目录中 展开
在D盘中创建文件test.txt,文件中内容为:hello Java,然后利用流把该文件拷贝到E盘根目录中 展开
9个回答
2017-06-15
展开全部
import java.io.*;
import java.util.Date;
import java.util.UUID;
/**
* 流对文件进行测试
*
* @author wlshi
* @create 2017-06-15 10:53
**/
public class test20170615 {
//生成文件路径
private static String path = "D:\\";
//文件路径+名称
private static String fileName;
/**
* 创建文件
*
* @param fileName 文件名称
* @param filecontent 文件内容
* @return 是否创建成功,成功则返回true
*/
public static boolean createFile(String fileName, String filecontent) {
Boolean bool = false;
File file = new File(path+fileName);
try {
//如果文件不存在,则创建新的文件
if (!file.exists()) {
file.createNewFile();
bool = true;
//创建文件成功后,写入内容到文件里
writeFileContent(path+fileName, filecontent);
}
} catch (Exception e) {
e.printStackTrace();
}
return bool;
}
/**
* 向文件中写入内容
*
* @param filepath 文件路径与名称
* @param newstr 写入的内容
* @return
* @throws IOException
*/
public static boolean writeFileContent(String filePath, String newStr) throws IOException {
Boolean bool = false;
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File file = new File(filePath);//文件路径(包括文件名称)
//将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
//文件原有内容
for (int i = 0; (temp = br.readLine()) != null; i++) {
buffer.append(temp);
// 行与行之间的分隔符 相当于“\n”
buffer = buffer.append(System.getProperty("line.separator"));
}
//新写入的行,换行
buffer.append(newStr + "\r\n");
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
//不要忘记关闭
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return bool;
}
/**
* 删除文件
*
* @param fileName 文件名称
* @return
*/
public static boolean delFile(String fileName) {
Boolean bool = false;
fileName = path + fileName + ".txt";
File file = new File(fileName);
try {
if (file.exists()) {
file.delete();
bool = true;
}
} catch (Exception e) {
// TODO: handle exception
}
return bool;
}
/**
* 复制文件
* @param file1
* @param file2
* @return
* @throws Exception
*/
public static long copyFile(String file1,String file2) throws Exception{
File f1=new File(file1);
File f2=new File(file2);
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
byte[] buffer=new byte[length];
while(true){
int ins=in.read(buffer);
if(ins==-1){
in.close();
out.flush();
out.close();
return new Date().getTime()-time;
}else
out.write(buffer,0,ins);
}
}
public static void main(String[] args)throws Exception {
UUID uuid = UUID.randomUUID();
createFile("myfile.txt", "1234567");
copyFile("D:\\myfile.txt","E:\\myfile.txt");
}
}
2017-06-15
展开全部
亲测可用,望采纳,不懂追问
public class FileCopy {
public static void main(String[] args) throws IOException{
// 构建源文件
File file = new File("E:" + File.separator + "text.txt");
OutputStream out = null;
// 根据文件创建文件的输出流
out = new FileOutputStream(file);
String message = "hello Java";
// 把内容转换成字节数组
byte[] data = message.getBytes();
// 向文件写入内容
out.write(data);
// 构建目标文件
File fileCopy = new File("E:" + File.separator + "HelloWorld.txt");
InputStream in = null;
// 目标文件不存在就创建
if (!(fileCopy.exists())) {
fileCopy.createNewFile();
}
// 源文件创建输入流
in = new FileInputStream(file);
// 目标文件创建输出流
OutputStream out2 = null;
out2 = new FileOutputStream(fileCopy, true);
// 创建字节数组
byte[] temp = new byte[1024];
int length = 0;
// 源文件读取一部分内容
while ((length = in.read(temp)) != -1) {
// 目标文件写入一部分内容
out2.write(temp, 0, length);
}
// 关闭文件输入输出流
in.close();
out.close();
out2.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCat
{
public static void main ( String[] args )
{
try
{
FileWriter fw = new FileWriter ("d:/test.txt");
fw.write ("hello Java");
fw.flush();
fw.close ();
FileInputStream fis = new FileInputStream ("d:/test.txt");
FileOutputStream fos = new FileOutputStream ("e:/test.txt");
byte[] b = new byte[1024];
while (-1 != fis.read (b))
{
fos.write (b);
}
fos.flush ();
fos.close ();
fis.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
//创建test.txt并向该文件写入内容hello world
createFile();
//复制d盘的文件到E盘
copyToE();
}
private static void copyToE() throws FileNotFoundException, IOException {
File file = new File("D://test.txt");
File file2 = new File("E://"+file.getName());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
int i ;
while((i = bis.read()) != -1){
bos.write(i);
}
bis.close();
bos.close();
}
private static void createFile() throws IOException, FileNotFoundException {
File file = new File("D://test.txt");
file.createNewFile();
String str = "hello world";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(str.getBytes());
bos.close();
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
//创建test.txt并向该文件写入内容hello world
createFile();
//复制d盘的文件到E盘
copyToE();
}
private static void copyToE() throws FileNotFoundException, IOException {
File file = new File("D://test.txt");
File file2 = new File("E://"+file.getName());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
int i ;
while((i = bis.read()) != -1){
bos.write(i);
}
bis.close();
bos.close();
}
private static void createFile() throws IOException, FileNotFoundException {
File file = new File("D://test.txt");
file.createNewFile();
String str = "hello world";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(str.getBytes());
bos.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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) {
String filePath = "e:/dzq.mp3";//文件地址
File f1 = new File(filePath);//来源文件对象
File f2 = new File("e:/copy_"+f1.getName());//复制后文件地址
if(f1.exists()){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(f1);
fos = new FileOutputStream(f2);
byte[] buf = new byte[1024];//缓存用的字节数组
int len = 0;
//循环读取
while((len=fis.read(buf))>0){
fos.write(buf,0,len);
}
System.out.println("复制完成!");
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
//关闭流
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
System.err.println("输入的来源文件找不到!");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询