将源文件复制到目标文件,同时输出源文件内容,需要提供一个源文件和目标文件路径参数(如果不存在则自动创建)

    public  static void  copyTxt(String sourceFile, String targetFile){
try{
String encoding = "GBK";
File file = new File(sourceFile);
if(file.exists() && file.isFile()){ //判断文件是否存在
FileInputStream input = new FileInputStream(file); //字节流
FileOutputStream output = new FileOutputStream(new File(targetFile)); InputStreamReader read = new InputStreamReader (input,encoding);//字符流
OutputStreamWriter write = new OutputStreamWriter(output,encoding); BufferedReader bufferedReader = new BufferedReader (read);//缓冲流
BufferedWriter bufferedWriter = new BufferedWriter(write);
String line = "";
while ( (line = bufferedReader.readLine() )!= null ){
System.out.println(line);
bufferedWriter.write(line + "\n");
}
bufferedReader.close();
bufferedWriter.close();
}else {
System.out.println("找不到指定的文件");
}
}catch(IOException e){
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}

Java——复制txt文件的更多相关文章

  1. Java读写txt文件

    1.Java读取txt文件 1.1.使用FileInputStream: public static String readFile(File file, String charset){ //设置默 ...

  2. Java读取txt文件

    package com.loongtao.general.crawler.slave.utils; import java.io.BufferedReader; import java.io.File ...

  3. java 读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  4. java读取TXT文件的方法

    java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...

  5. java读取txt文件内容

    package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...

  6. java读取txt文件的2中方法---并将内容(每一行以固定的字符分割切成2段)存到map中去

    #java读取txt文件的第一种方法 /** * 方法:readTxt * 功能:读取txt文件并把txt文件的内容---每一行作为一个字符串加入到List中去 * 参数:txt文件的地址 * 返回: ...

  7. Java读取txt文件信息并操作。

    一.java读取txt文件内容 import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.Fi ...

  8. JAVA获取txt文件内容

    JAVA 读取txt文件内容 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文件字符编码即可. public class txttest { /** * 读 ...

  9. Java读取txt文件、excel文件的方法

    Java读取txt文件.excel文件的方法 1.读取txt文件 public static String getFileContent(String filePath,String charset) ...

随机推荐

  1. MVC Ajax Helpers

    在MVC中要实现Ajax有很多的方式,有微软自己的MicrosoftAjax,也可以用JQuery的AJax来实现,如果对其他的JavaScript框架熟悉,还可以采用其他的实现方案,比如说Proto ...

  2. bzoj2134

    题解: 每一题对的概率为min(a[i],a[i+1])/a[i]/a[i+1]; 即可 代码: #include<bits/stdc++.h> using namespace std; ...

  3. 『转』Dr.Web Security Space 8 – 免费3个月

    简短的测试五个问题,任意回答问题,都将获得Dr.Web Security Suite 3个月免费许可证以及大蜘蛛企业安全套件2个月来保护整个公司!活动地址:https://www.drweb.com/ ...

  4. mongdb分布式

    搭建高可用mongodb集群(一)——配置mongodb Posted on 17 十一月, 2013 by lanceyan | 21 Replies 在大数据的时代,传统的关系型数据库要能更高的服 ...

  5. 《DSP using MATLAB》Problem 2.8

    1.代码: 从MATLAB官方网上下载的. %*************************************************************************% %A ...

  6. 《FDTD electromagnetic field using MATLAB》读书笔记 Figure 1.2

    函数f(x)用采样间隔Δx=π/5进行采样,使用向前差商.向后差商和中心差商三种公式来近似一阶导数. 书中代码: %% ---------------------------------------- ...

  7. WinSCP一个好用的连接linux服务器的

    用虚拟机ssh登陆远程服务器,终端命令copy本地文件到服务器简直弱爆了. 不然用win下的WinSCP,牛逼到爆了.操作跟FTP软件差不多

  8. Windows Server Core Command (管理服务器核心的具体操作命令)

    从 Windows Server 2008 开始,管理员可以选择安装具有特定功能但不包含任何不必要功能的 Windows Server 的最小安装服务器核心(Server Core),它为一些特定服务 ...

  9. 基于ffmpegSDK的开发

    #include <stdio.h> #include <libavutil/avutil.h> #include <libavcodec/avcodec.h> # ...

  10. BZOJ1252:序列终结者

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...