- class ReadWriter implements Runnable{
- @Override
- public void run() {
- FileWriter fw=null; //写入流对象
- FileReader fr=null; // 读取流对象
- int len=0;
- char [] chars=new char[1024];
- try{
- fw=new FileWriter("IOCopy.txt");
- fr=new FileReader("IO.txt");
- while ((len=fr.read(chars))!=-1){
- fw.write(chars,0,len);
- fw.flush();
- }
- }catch (IOException e){
- e.printStackTrace();
- } finally {
- if (fr != null) {
- try {
- fr.close(); //关闭读取流
- } catch (IOException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- if (fw != null) {
- try {
- fw.close(); //关闭写入流
- } catch (IOException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- }
- }
- }
- public class IOThreadDemo {
- public static void main(String[] args) {
- /* ReadWriter rw=new ReadWriter();
- Thread t1=new Thread(rw);
- Thread t2=new Thread(rw);
- t1.start();
- t2.start();*/
- ExecutorService exec=Executors.newCachedThreadPool();
- exec.submit(new ReadWriter());
- exec.shutdown();
- }
- }
复制代码 同标题,如何处理复制后文本打开是乱码?
|
|