-
2021-03-14 18:18:10
public static void main(String[] args) throws IOException {
File file = new File("c:/aaa.txt");
if (!file.exists()) {
file.createNewFile();
}
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
out.write("班级\t学号\t姓名\t年龄\t籍贯\r\n");
out.write("355\t1512\tXXX\t25\tXXXX\n");
out.close();
BufferedReader in = new BufferedReader(new FileReader(file));
String s = "";
while ((s = in.readLine()) != null) {
System.out.println(s);
}
in.close();
}
取消
评论
更多相关内容 -
Python 在d盘根目录下,创建一个文件夹a,在该文件夹下,创建a0-a99一百个文件夹。 然后再在a0中创建b0....
2022-02-14 14:23:51在d盘根目录下,创建一个文件夹a,在该文件夹下,创建a0-a99一百个文件夹。然后再在a0中创建b0.txt,a1中创建b1.txt,以此类推到b99。最后将a文件下所有的文件删除。1.创建
import os.path if os.path.exists("D:/a"): print("Error,D盘下的a目录已经存在") else: os.mkdir("D:/a") path="D:/a" for i in range(100): os.mkdir(path+'/'+'a'+str(i)) print("在D:/a目录下创建a"+str(i)+'成功') open(path+'/'+'a'+str(i)+'/'+'b'+str(i)+'.txt','w') print("在D:/a目录中的a" + str(i) + '目录下创建b'+str(i)+'.txt文件成功')
运行结果
2.删除
import os.path path="D:/a" for i in range(100): os.remove(path+'/'+'a'+str(i)+'/'+'b'+str(i)+'.txt') os.rmdir(path+'/'+'a'+str(i)) print("删除D:/a目录下的所有目录以及txt文件成功")
运行结果
-
JAVA编写程序,把C盘根目录的文本test.dat复制到D盘根目录
2021-03-09 04:43:24} } 实现方法:在第一种实现方法基础上对输入输出流获得其管道,然后分批次的从f1的管道中像f2的管道中输入数据每次输入的数据最大为2MB 方法3:内存文件景象写(读文件没有使用文件景象,有兴趣的可以回去试试,,我就不...展开全部
使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢?
java NIO 性能提升.
第一种方法:古老636f707962616964757a686964616f31333264643061的方式
Java代码
public static long forJava(File f1,File f2) throws Exception{
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 long forJava(File f1,File f2) throws Exception{
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);
}
}
方法的2参数分别是原始文件,和拷贝的目的文件.这里不做过多介绍.
实现方法很简单,分别对2个文件构建输入输出流,并且使用一个字节数组作为我们内存的缓存器, 然后使用流从f1 中读出数据到缓存里,在将缓存数据写到f2里面去.这里的缓存是2MB的字节数组
第2种方法:使用NIO中的管道到管道传输
Java代码
public static long forTransfer(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<20971520)
length=(int)(inC.size()-inC.position());
else
length=20971520;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}
public static long forTransfer(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
int i=0;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())<20971520)
length=(int)(inC.size()-inC.position());
else
length=20971520;
inC.transferTo(inC.position(),length,outC);
inC.position(inC.position()+length);
i++;
}
}
实现方法:在第一种实现方法基础上对输入输出流获得其管道,然后分批次的从f1的管道中像f2的管道中输入数据每次输入的数据最大为2MB
方法3:内存文件景象写(读文件没有使用文件景象,有兴趣的可以回去试试,,我就不试了,估计会更快)
Java代码
public static long forImage(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
RandomAccessFile out=new RandomAccessFile(f2,"rw");
FileChannel inC=in.getChannel();
MappedByteBuffer outC=null;
MappedByteBuffer inbuffer=null;
byte[] b=new byte[length];
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.force();
out.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())
length=(int)(inC.size()-inC.position());
}else{
length=20971520;
}
b=new byte[length];
inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);
inbuffer.load();
inbuffer.get(b);
outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);
inC.position(b.length+inC.position());
outC.put(b);
outC.force();
}
}
public static long forImage(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
RandomAccessFile out=new RandomAccessFile(f2,"rw");
FileChannel inC=in.getChannel();
MappedByteBuffer outC=null;
MappedByteBuffer inbuffer=null;
byte[] b=new byte[length];
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.force();
out.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())
length=(int)(inC.size()-inC.position());
}else{
length=20971520;
}
b=new byte[length];
inbuffer=inC.map(MapMode.READ_ONLY,inC.position(),length);
inbuffer.load();
inbuffer.get(b);
outC=out.getChannel().map(MapMode.READ_WRITE,inC.position(),length);
inC.position(b.length+inC.position());
outC.put(b);
outC.force();
}
}
实现方法:跟伤2个例子不一样,这里写文件流没有使用管道而是使用内存文件映射(假设文件f2在内存中).在循环中从f1的管道中读取数据到字节数组里,然后在像内存映射的f2文件中写数据.
第4种方法:管道对管道
Java代码
public static long forChannel(File f1,File f2) throws Exception{
long time=new Date().getTime();
int length=2097152;
FileInputStream in=new FileInputStream(f1);
FileOutputStream out=new FileOutputStream(f2);
FileChannel inC=in.getChannel();
FileChannel outC=out.getChannel();
ByteBuffer b=null;
while(true){
if(inC.position()==inC.size()){
inC.close();
outC.close();
return new Date().getTime()-time;
}
if((inC.size()-inC.position())
length=(int)(inC.size()-inC.position());
}else
length=2097152;
b=ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
}
}
注:参数中的File可以这样定义:
FIle f1 = new File("C:\\test.dat");
File f2 = new File("D:\\test.dat");
已赞过
已踩过<
你对这个回答的评价是?
评论
收起
-
在D盘中创建文件test.txt,文件中内容为:hello Java,然后利用流把该文件拷贝到E盘根目录中
2021-03-09 04:43:41// 在D盘中创建文件test.txt,文件中的内容为:“hello Java”File file = new File("D:/test.txt");StringBuilder builder = new StringBuilder();builder.append("hello java");OutputStreamWriter osw = null;try ...// 在D盘中创建文件test.txt,文件中的内容为:“hello Java”
File file = new File("D:/test.txt");
StringBuilder builder = new StringBuilder();
builder.append("hello java");
OutputStreamWriter osw = null;
try {
osw = new FileWriter(file);
osw.write(builder.toString());
} catch (IOException e) {
e.printStackTrace();
} finally { // 最后一定要关闭流
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 利用流把该文件拷贝到E盘根目录下
InputStream ips = null;
OutputStream ops = null;
try {
ips = new FileInputStream(file);// 源文件
byte[] buffer = new byte[1024]; // 定义一个缓冲数组
ops = new FileOutputStream("E:/" + file.getName());// 目标文件
// 如果没有读到结尾就继续读,每次读指定的字节数
for (int len = 0; (len = ips.read(buffer)) != -1;) {
ops.write(buffer, 0, len); // 每次写出实际读取到长度
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally { // 最后关闭流
if (ips != null) {
try {
ips.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ops != null) {
try {
ops.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
简单来写:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
public class Change {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("D:/tets.txt");
fw.write("hello java");
fw.close();
FileInputStream fis = new FileInputStream("d:/tets.txt");
FileOutputStream fos = new FileOutputStream("E:/tets.txt");
byte[] b = new byte[1024];
while(fis.read(b)!=-1)
{
fos.write(b);
}
fos.close();
fis.close();
}
}
-
编写程序,在 D 盘根目录下创建一个文本文件 test.txt ,并向其中写入字符串 hello world 。 (10.0分)_学小易...
2021-01-29 12:37:47【计算题】• 编写函数,模拟内置函数all ()、any() 和zip ()。 (4.0分)【计算题】编程求 200 以内能被 17 整除的最大正整数。 (4.0分)【计算题】编写程序,计算...函数 lingxing(n) ,参数 n 为菱形边长 * * * * * * *... -
利用fopen函数,在d盘根目录下新建文件“Hello.txt”,已知有数组a[100] = “Hi, Lucy\nHi, Lily\nHi, ...
2022-01-09 12:01:16 利用fopen函数,在d盘根目录下新建文件“Hello.txt”,已知有数组a[100] = “Hi, Lucy!\nHi, Lily!\nHi, Ecuter!”,将数组a的内容写入到txt文件当中。 -
编程实现在D盘目录下创建一个JavaTest文件夹
2021-02-12 13:08:56展开全部importjava.math.BigInteger;publicclassTest{publicstaticStringbaseString(intnum,intbase){Stringstr="",digit="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";if(num==0){return"";}else{... -
Windows操作: (1)在C:盘根目录下建立SUB文件夹,在SUB文件夹下建立SUB1子文件夹和SUB2子文件夹。...
2021-05-24 06:04:27Windows操作:(1)在C:盘根目录下建立SUB文件夹.在SUB文件夹下建立SUBl子文件夹和SUB2子Windows操作:(1)在C:盘根目录下建立SUB文件夹.在SUB文件夹下建立SUBl子文件夹和SUB2子文件夹。(2)查找note.exe文件。(3)... -
汇编语言: 在D盘根目录建立一个文件abc.txt,第1次向文件中写入“123456”六个字符,第2次 增加“abcdefg”...
2017-06-05 19:47:07在D盘根目录建立一个文件abc.txt,第1次向文件中写入“123456”六个字符,第2次 增加“abcdefg”几个字符。 data segmentstring1 db '123456','$' string2 db 'abcdefg','$'file db 'a\abc.txt',0 fh dw ?copybuf ... -
python--编程在E盘根目录下创建一个文本文件“悯农.txt”。在文件中写入诗句:锄禾日当午,汗滴禾下土。谁...
2021-07-07 13:10:10编程在E盘根目录下创建一个文本文件“悯农.txt”。在文件中写入诗句:锄禾日当午,汗滴禾下土。谁知盘中餐,粒粒皆辛苦。从“悯农”文件中读出所有行,以每行为元素形成一个列表ls,并输出列表中的内容。–Python ... -
在dzqc目录下创建一个123.txt
2019-04-14 19:40:11touch /dzqc/123.txt 在dzqc目录下创建一个123.txt touch 1.txt 在当前路径下创建1.txt文件 -
在D盘中创建文件test.txt,文件中内容为:hello Java,然后利用流把该文件拷贝到
2021-03-09 04:44:34/** * 创建文件 * * @param fileName 文件名称 * @param filecontent 文件内容 * @return 是否创建成功,成功则返回true */ public static boolean createFile(String fileName, String filecontent... -
python根据txt文本批量创建文件夹
2020-12-10 22:04:29前言前言:想写这个代码的原因是因为实习的时候需要根据表格名创建对应的文件夹,如果只是很少个数文件夹的话,ctrl+shift+n还可以接受吧,可是一次就要创建几百个文件夹,这就有点方方了。所以我写了一些代码解决... -
python中如何创建一个txt文件
2021-02-10 04:20:52如果以只写模式打开一个文件,那么该函数会在文件不存在时创建一个文件。(推荐教程:Python入门教程)语法:open(name[,mode[,buffering]])参数:name : 一个包含了你要访问的文件名称的字符串值。mode : mode 决定了... -
python文件操作2:在指定目录下查找指定后缀名的文件
2021-05-27 17:18:25在指定目录下查找指定后缀名的文件,要查找所有子目录,返回文件路径名列表 import os def get_full_filelist(base_dir='.', target_ext='') -> list: fname_list = [] # 用于记录文件名的列表 for ... -
python查找目录下指定扩展名的文件实例
2020-01-22 19:08:51原文地址见上 本文实例讲述了python查找目录下指定扩展名的文件。...这里使用python查找当前目录下的扩展名为.txt的文件 ? 1 2 3 4 5 6 7 import os items... -
计算机应用基础本科操作题
2021-07-21 02:31:31计算机应用基础本科操作题2、3、2 操作题例题与解析【例2-11】(1)在C盘的根目录下建立一个新文件夹,并取名为“我的新文件夹”;(2)将此文件夹的快捷方式放在桌面上。【答案与解析】有如下4个操作步骤:①在“资源管理... -
第二章上机编程作业 在电脑 D (或 E )盘的根目录下建立以 "序号- 自己学号 + 姓名 " 命名,如 “22- 张三...
2021-02-12 05:25:46第二章上机编程作业在电脑D(或E)盘的根目录下建立以"序号-自己学号+姓名"命名,如“22-张三”的文件夹,然后在该文件夹下再建立名称为“第2章”的子文件夹。下面的每个练习分别以ex020*.frm和ex020*.vbp为文件名保存... -
java代码在D盘下创建一个mytemp文件夹,显示D盘下所有的.Java文件,包括D盘的子文件夹下的.java文件,并...
2020-06-17 20:26:35利用java代码在D盘下创建一个mytemp文件夹 显示D盘下所有的.Java文件,包括D盘的子文件夹下的.java文件 把上述显示的文件都复制到mytemp文件夹中 private static void work12() throws IOException { /** *... -
在D盘目录下创建一个HelloWord.txt文件,分别用PrintWriter,BufferWriter向HelloWorde2.txt,HelloWorde3....
2015-09-19 22:32:44package darker; import java.io.*; public class six { public static void main(String[] args) throws IOException { ... File file1=new File("D:/HelloWord1.txt"); if(!(file1.exists())){ file -
在d盘中创建一个文件夹 在文件夹里创建三个txt文本
2019-03-23 23:04:00在d盘创建一个文件夹aaa madir()创建文件夹目录 15 File file = new File("d:\\aaa" ); 16 boolean mkdir = file.mkdir(); 17 System.out.println(mkdir); 18 // 在d盘的文件夹aaa里面创建三个... -
创建目录及txt文件并写入内容
2021-08-04 10:01:27DateTime dataTime = DateTime.Now; string now = dataTime.Month.ToString().PadLeft(2,'0') + dataTime.Day.ToString... string path = string.Format(@"D:\干活\{0}", now); if (!Directory.Exists(path)) { Dir -
liunx文件目录操作命令
2021-03-22 21:44:44home目录,每个用户都在这目录下有一个子目录 例子 用户名 bb 那么那他的目录为 /home/bb opt目录,安装第三方软件和插件 root目录,超级用户root的家目录 bin目录和sbin目录都是装的二进制文件,s 是system系统的... -
将d盘根目录下的troydll.dll插入到ID为4000的进程中
2009-09-06 12:44:00//将d盘根目录下的troydll.dll插入到ID为4000的进程中:#include <windows.h>#include <stdlib.h>#include <stdio.h>PDWORD pdwThreadId; HANDLE hRemoteThread, hRemoteProcess;DWORD fdwCreate,... -
【笔记】Python 实现目录及子目录文件的查找
2016-09-28 22:01:10今天绕这个东西弄了一天,虽然很简单,但还是总结下吧1. 问题描述来自廖雪峰网站,操作文件和目录一节的... 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。 -
新建以自己学号为名字的目录,并在此目录中生成(学号%1000)个文件,并证明生成了相应个文件。...
2017-04-16 00:13:47新建以自己学号为名字的目录,并在此目录中生成(学号%1000)个文件,并证明生成了相应个文件。#!/bin/bash#by tuzinum=3120210424let n=$num%1000mkdir `echo $num`for ((i=1;i<=$n;i++))do touch $num/"file$i... -
JAVA用File创建一个多级目录a/b/c/d/e/f,然后在每一个目录里面添加一些文件和目录
2021-02-27 20:13:21展开全部以下为32313133353236313431303231363533e4b893e5b19e31333365663439一些基本操作importjava.io.*;publicclassTest{publicstaticvoidmain(String[]args)throwsIOException{Filefile=newFile("D:/test/a/b/..... -
Dos命令
2021-08-26 16:18:28DOS的来历 Dos即Disk Operating System 中文“磁盘操作系统” 简单点说就是 win+shift cmd DOS下的文件名 .exe 可执行文件 .com 可执行文件 .bat 批处理文件 .txt 纯文本文件 ...在DOS下 为了让D -
【Oracle数据库】创建、增删查改表空间和用户练习
2021-04-19 19:17:51创建表空间,数据文件命名为tablespace01,存放在D盘根目录,大小为10M; create tablespace tablespace01 datafile 'D:/tablespace01.dbf' size 10M; 创建表空间,数据文件命名为tablespace02,存放在D盘test...