import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
public class Test{
public static void main(String[] args) throws Exception {
//创建2个文件
File file1 = new File("D://test1.txt");
File file2 = new File("d://test2.txt");
file1.createNewFile();
file2.createNewFile();
//从键盘输入的字符写入test1.txt
FileWriter fout = new FileWriter(file1);
String str=null;
System.out.println("Please input a String:");
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
str = fin.readLine();
System.out.println(str);
fout.write(str,0,str.length());
fout.flush();
fout.close();
fin.close();
//复制文件test1.txt的内容到test2.txt
//为了让你明白通道所以我这里用了2种读写入文件的方法
new Exam2().copy("D://test1.txt", "d://test2.txt");
}
/**
* 复制文件封装类
* @param url1
* @param url2
* @throws Exception
*/
public void copy(String url1, String url2) throws Exception {
FileInputStream input = new FileInputStream(url1);
FileChannel filechannel = input.getChannel();
FileOutputStream output = new FileOutputStream(url2);
FileChannel filechannel2 = output.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024 *
;
while (filechannel.read(buf) != -1) {
buf.flip();
filechannel2.write(buf);
buf.clear();
}
input.close();
filechannel.close();
output.close();
filechannel2.close();
}
}
分享到:
相关推荐
java写入Excel所需要的jar包,源码在博客,各位大表哥欢迎指正
java写入txt:两种方法,1:利用PrintWriter 类2.FileOutputStream输出流
java写入excel图片操作 包含需要的类库和相应的代码
POI处理excel文件,主要包括Excel的读取、写入,各种数据格式处理、单元格合并、注释、下拉列表及单元格的边框、背景色、宽度调整等。
java写入字符的简单代码,可以从中学到一点JAVA基础知识哦!
使用Java写入大数据文件的方法,分文件写入。
java 写入 excle 、pdf、 word资料
Java写入文件的几种方法共8页.pdf.zip
Java写入MySQL中文乱码解决方法.docx
java操作excel03,07,用于将数据写入到这两种版本的excel文件的,其他对于excel很好的解决了导出大数据量的时候出现的内存溢出异常问题
利用java写入和存储XML。。。。。。。。。。。。。。
java技术:java写入文件的三种方式 欧柏泰克
Java写入文件、读取文件,复制、追加、修改文件最后修改时间、时间格式转换、查看端口使用情况
java读取和写入EXCEL文件,在myeclipse平台上运行!!
java语言,根据办公软件word中标签的位置, 写入相应数据字段。java操作word。
java读取写入2007版excel所用的包
详细介绍了java语言中写文件操作的几种不同的方法,针对不同的需求可以灵活选择。
java利用io技术创建文件夹、读取txt文件、写入txt文件(覆盖、不覆盖均有)
java日志写入类,根据网上代码修改,可自定义日志写入文件夹