java写文件路径(java程序运行步骤)
程序员文章站
2022-07-10 20:31:39
0x01:fileinputstream/fileoutputstream字节流进行文件的复制privatestaticvoidstreamcopyfile(filesrcfile,filedesfi...
0x01:
fileinputstream/fileoutputstream字节流进行文件的复制
private static void streamcopyfile(file srcfile, file desfile) {
try{
// 使用字节流进行文件复制
fileinputstream fi = new fileinputstream(srcfile);
fileoutputstream fo = new fileoutputstream(desfile);
integer by = 0;
//一次读取一个字节
while((by = fi.read()) != -1) {
fo.write(by);
}
fi.close();
fo.close();
}catch(exception e){
e.printstacktrace();
}
}
0x02:
bufferedinputstream/bufferedoutputstream高效字节流进行复制文件
private static void bufferedstreamcopyfile(file srcfile, file desfile){
try{
// 使用缓冲字节流进行文件复制
bufferedinputstream bis = new bufferedinputstream(new fileinputstream(srcfile));
bufferedoutputstream bos = new bufferedoutputstream(new fileoutputstream(desfile));
byte[] b = new byte[1024];
integer len = 0;
//一次读取1024字节的数据
while((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bis.close();
bos.close();
}catch(exception e){
e.printstacktrace();
}
}
0x03: filereader/filewriter字符流进行文件复制文件
private static void readerwritercopyfile(file srcfile, file desfile){
try{
// 使用字符流进行文件复制,注意:字符流只能复制只含有汉字的文件
filereader fr = new filereader(srcfile);
filewriter fw = new filewriter(desfile);
integer by = 0;
while((by = fr.read()) != -1) {
fw.write(by);
}
fr.close();
fw.close();
}catch(exception e){
e.printstacktrace();
}
}
0x04:
bufferedreader/bufferedwriter高效字符流进行文件复制
private static void bufferedreaderwritercopyfile(file srcfile, file desfile){
try{
// 使用带缓冲区的高效字符流进行文件复制
bufferedreader br = new bufferedreader(new filereader(srcfile));
bufferedwriter bw = new bufferedwriter(new filewriter(desfile));
char[] c = new char[1024];
integer len = 0;
while((len = br.read(c)) != -1) {
bw.write(c, 0, len);
}
//方式二
/*
string s = null;
while((s = br.readline()) != null) {
bw.write(s);
bw.newline();
}
*/
br.close();
bw.close();
}catch(exception e){
e.printstacktrace();
}
}
0x05: nio实现文件拷贝(用transferto的实现 或者transferfrom的实现)
public static void niocopyfile(string source,string target) {
try{
//1.采用randomaccessfile双向通道完成,rw表示具有读写权限
randomaccessfile fromfile = new randomaccessfile(source,"rw");
filechannel fromchannel = fromfile.getchannel();
randomaccessfile tofile = new randomaccessfile(target,"rw");
filechannel tochannel = tofile.getchannel();
long count = fromchannel.size();
while (count > 0) {
long transferred = fromchannel.transferto(fromchannel.position(), count, tochannel);
count -= transferred;
}
if(fromfile!=null) {
fromfile.close();
}
if(fromchannel!=null) {
fromchannel.close();
}
}catch(exception e){
e.printstacktrace();
}
}
0x06: java.nio.file.files.copy()实现文件拷贝,其中第三个参数决定是否覆盖
public static void copyfile(string source,string target){
path sourcepath = paths.get(source);
path destinationpath = paths.get(target);
try {
files.copy(sourcepath, destinationpath,
standardcopyoption.replace_existing);
} catch (ioexception e) {
e.printstacktrace();
}
}
推荐阅读
-
Java利用exe4j工具生成exe文件实例演示,IntelliJ IDEA将项目转化为jar包方法,运行生成后的程序弹出exe4j提示处理
-
eclipse的使用和运行第一个java程序(步骤详细)
-
获取Java程序运行的路径 | 获取当前jar包的路径
-
java写文件路径(java程序运行步骤)
-
IntelliJ IDEA 创建 Java 项目及创建 Java 文件并运行的详细步骤
-
出现 “java”不是内部或外部命令,也不是可运行程序或批处理文件的问题
-
jdk32位安装教程(java程序运行步骤)
-
打包java项目为jar文件通过exe4j工具生成exe的可运行程序
-
eclipse如何导入web项目并运行(java程序运行步骤)
-
Java基础知识(JDK、JRE、JVM; 编写java小程序之文件命名、编译javac、运行java)