java对文件的基本操作
package cn.edu.fhj.day009.FileDemo; import java.io.File;
import java.io.IOException; public class FileDemo { public static void main(String[] args) throws IOException { // 将路径描述成File对象
// File file = new File("d:/java_fd_test/fileDemo.txt");
File file = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm");
boolean exists = file.exists(); // 如果路径所表示的文件或者文件夹存在,则返回true
System.out.println(exists); // 判断该file是文件夹还是文件
boolean directory = file.isDirectory();
System.out.println(directory); // true boolean ifFile = file.isFile();
System.out.println(ifFile); // false // 获取文件的绝对路径
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath); // 可以获取文件名或文件夹名
String name2 = file.getName();
System.out.println(name2); File file2 = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt");
ifFile = file2.isFile(); // true
System.out.println(ifFile); // 获取文件名
String name = file2.getName();
System.out.println(name); // 获取上一级目录的file对象
File parentFile = file2.getParentFile();
System.out.println(parentFile.getAbsolutePath()); // 获取上一级目录的路径字符串
String parent = file2.getParent();
System.out.println(parent); // 获取文件长度 字节(8个bit-- 二进制位)
long length = file2.length();
System.out.println(length); System.out.println("------------------------"); // 获取指定目录下的子节点的名称字符串
String[] list = file.list();
for (String s : list) {
System.out.println(s);
} System.out.println("------------------------"); // 获取指定目录下的子节点的File描述对象
File[] listFiles = file.listFiles();
for (File f : listFiles) {
System.out.println(f.getAbsolutePath());
} System.out.println("------------------------"); // 创建一个文件夹
File f = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz");
// boolean mkdir = f.mkdir(); // 不能创建多级目录
// System.out.println(mkdir); // boolean mkdirs = f.mkdirs(); // 可以创建多级目录
// System.out.println(mkdirs);
//
// // 创建文件
File file3 = new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls.txt");
boolean createNewFile = file3.createNewFile();
System.out.println(createNewFile); // 重命名文件:其实可以把路径都给改了
file3.renameTo(new File(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls001.txt")); // 删除文件
boolean delete = file3.delete();
System.out.println(delete); } }
FileOutputStreamDemo文件的写
package cn.edu.fhj.day009.FileDemo; import java.io.FileOutputStream; public class FileOutputStreamDemo { public static void main(String[] args) throws Exception { // 覆盖的方式写数据
FileOutputStream fos = new FileOutputStream(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); String s = "a你好";
byte[] bytes = s.getBytes();
fos.write(bytes);
// 将字符串按指定编码集编码--》将信息转成二进制数 fos.write(bytes); // 这样写入的数据,会将文件中的原数据覆盖 // 追加的方式写数据:如果要往一个文件中追加数据,则在FileOutputStream的构造参数中多传一个true
FileOutputStream fos2 = new FileOutputStream(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt",
true);
fos2.write(",sb".getBytes("UTF-8"));
fos2.close(); /**
* 第一句和后两句话写到文件中的数据完全相同
*/
fos.write("我用一生一世为你祈祷".getBytes()); // .getBytes()编码的过程
fos.write((byte) 49);
fos.write((byte) 51); /**
* 这两句话写到文件中的数据完全相同
*/
// fos.write((byte)13);
// fos.write("\r".getBytes()); fos.close(); }
}
FileInputStreamDemo的读
package cn.edu.fhj.day009.FileDemo; import java.io.FileInputStream;
import java.io.InputStream; public class FileInputStreamDemo { public static void main(String[] args) throws Exception { // 要读文件,首先要构造一个FileInputStream对象
InputStream fis = new FileInputStream(
"F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); /**
* 把数从文件中读取出来 如何读取字符
*/
// FileInputStream是一种字节流,是按照一个一个字节去文件中取数据的
// 手动一个字节一个字节地读取
/*
* int read = fis.read();
*
* System.out.println(read);
*
* read = fis.read(); System.out.println(read);
*/ /**
* 利用fis读到文件末尾后会返回-1的特性,来用循环进行读取
*/
int read = 0;
/*
* while((read=fis.read())!=-1) { System.out.println(read); }
*/ System.out.println("-------------------");
/**
* 如果我要读出数据(文本文件中的数据其实就是字符) 过程是:还是先读数,然后按照码表,将这个数转成字符
*
*/
/*
* read = 0; while((read=fis.read())!=-1) { //
* char就代表一个英文字符,而且使用的是ascII码表规则 char c = (char)read;
* System.out.println(c); }
*/ /**
* 一次读取多个字节然后转成某种数据类型 read(buf)方法,一次读取buf长度个字节数据,并且读到的数据直接填入了buf数组中
*/
/*
* byte[] buf = new byte[8]; int num = fis.read(buf); // 返回的是真实读到的字节数量
* String string = new String(buf,2,5); // 利用二进制的byte数组来转成字符串
* System.out.println(string);
*/ /**
* 用while循环来反复读取
*/
int num = 0;
byte[] buf = new byte[8];
while ((num = fis.read(buf)) != -1) {
System.out.println(new String(buf, 0, num));
} // 关流
fis.close();
} }
java对文件的基本操作的更多相关文章
- java写文件的基本操作
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOExce ...
- java读取文件的基本操作
import java.io.FileInputStream; /** * 使用FileInputStream读取文件 */ public class FileRead { /** * @param ...
- java之文件基本操作
java之文件基本操作 1 使用 BufferedReader 在控制台读取字符 public static void readChar() throws IOException{ char c; I ...
- Java api 入门教程 之 JAVA的文件操作
I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程的一个基本 ...
- servlet中Java连接数据库后的基本操作
servlet中Java连接数据库后的基本操作 在eclipse中新建一个工程:login 在Server中新建一个服务器,基本的操作不用说了,在前两天的笔记中可以找到; 需要知道数据库的用户名和密码 ...
- JAVA的文件操作【转】
11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...
- HDFS文件的基本操作
HDFS文件的基本操作: package wjn; import java.io.BufferedInputStream; import java.io.BufferedReader; import ...
- I/O流以及文件的基本操作
文件操作: 文件操作其实就是一个FIle类:我们学习文件操作就是学习File类中的方法: 文件基操: 第一部分:学习文件的基本操作(先扒源码以及文档) Constructor Description ...
- java创建文件和目录
java创建文件和目录 2013-09-04 12:56 99933人阅读 评论(7) 收藏 举报 分类: JAVA基础(10) 版权声明:本文为博主原创文章,未经博主允许不得转载. 创建文件和目 ...
随机推荐
- springboot-文件上传xls及POI操作Excel
1.pom导入依赖文件 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-o ...
- Capsules for Object Segmentation(理解)
0 - 背景 今年来卷积网络在计算机视觉任务上取得的显著成果,但仍然存在一些问题.去年Hinton等人提出了使用动态路由的新型网络结构——胶囊网络来解决卷积网络的不足,该新型结构在手写体识别以及小图像 ...
- android的nfc卡模拟开发
这是andorid官方文档对于卡模拟方式的描述: https://developer.android.google.cn/guide/topics/connectivity/nfc/hce 有两种方式 ...
- Saltstack自动化操作记录(1)-环境部署【转】
早期运维工作中用过稍微复杂的Puppet,下面介绍下更为简单实用的Saltstack自动化运维的使用. Saltstack知多少Saltstack是一种全新的基础设施管理方式,是一个服务器基础架构集中 ...
- stat 文件三个时间点
Linux文件3个时间点(access time,modify time,change time) access time:表示最后一次访问(仅仅是访问,没有改动)文件的时间. 注意:至于为什么会出现 ...
- Mac本地搭建kubernetes环境
前言:之前在windows上面的虚拟机上面手工搭建了kubernetes集群,但是环境被破坏了,最近想要继续学习k8s,手工搭建太费事,所以选择了minikube,完全能够满足个人的需求,其实在Win ...
- 如何用java实现一个p2p种子搜索(4)-种子获取
种子获取 在上一篇中我们已经可以获取到dht网络中的infohash了,所以我们只需要通过infohash来获取到种子,最后获取种子里面的文件名,然后和获取到的infohash建立对应关系,那么我们的 ...
- C#接口的简单创建及其用法
我初次接触接口(Interface),对接口的作用有点迷茫,C#接口中包含方法.属性.索引器和事件的声明,但常用的接口中一般就是方法和属性,然而接口中并没有方法的具体实现代码(不能提供任何成员实现), ...
- shutil&shelve
https://www.cnblogs.com/xiangsikai/p/7787101.html http://www.cnblogs.com/wupeiqi/articles/4963027.ht ...
- STM32F0使用LL库实现Modbus通讯
在本次项目中,限于空间要求我们选用了STM32F030F4作为控制芯片.这款MCU不但封装紧凑,而且自带的Flash空间也非常有限,所以我们选择了LL库实现.本篇将说明基于LL实现USART通讯. 1 ...