java IO之File基本操作
public static void main(String[] args) {
// TODO Auto-generated method stub
//"G:\\JAVA\\test\\test.txt"
createNewFile();
showSeparator();
useSepartor();
mkNewDir();
showAllNoSub();
showAllWithSub(new File("G:"+File.separator+"JAVA"+File.separator+"test"+File.separator));
}
/*
* 1.创建一个新文件
*/
public static void createNewFile() {
File file = new File("G:\\JAVA\\test\\test.txt");
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 2.file类的常量separator,patSeparator。文件分割符 //对于不同的系统分隔符孚不一样
*/
public static void showSeparator() {
System.out.println(File.separator); // \
System.out.println(File.pathSeparator); // ;
}
/*
* 3.删除文件,用separator让程序更健壮
*/
public static void useSepartor() {
String fileName = "G:" + File.separator + "JAVA" + File.separator
+ "test" + File.separator + "test.txt";
File file = new File(fileName);
if (file.exists())
file.delete();
else
System.out.println("文件不存在");
}
/*
* 4.创建一个文件夹
*/
public static void mkNewDir()
{
String filePath= "G:" + File.separator + "JAVA" + File.separator
+ "test" + File.separator + "newDir";
File file=new File(filePath);
if(file.isDirectory())
System.out.println("dir exit");
else
System.out.println(file.mkdir());
}
/*
*5. 列出所在目录的所有文件包括隐藏文件(但是不显示子文件)
*/
public static void showAllNoSub()
{
String filePath="G:"+File.separator+"JAVA"+File.separator+"test";
File file=new File(filePath);
String [] all=file.list();
for(String s:all)
{
System.out.println(s);
}
}
/*
*6. 用递归算法,列出所在目录的所有文件包括隐藏文件,包括子文件
*/
public static void showAllWithSub(File file)
{
if(file.isFile()) //判断是文件
System.out.println(" File:"+file.getPath());
else if(file.isDirectory()) //判断是文件夹
{
System.out.println("Dir:"+file.getPath());
File [] allfile=file.listFiles();
for(File f : allfile)
{
showAllWithSub(f);
}
}
}
java IO之File基本操作的更多相关文章
- [Storm] java.io.FileNotFoundException: File '../stormconf.ser' does not exist
This bug will kill supervisors Affects Version/s: 0.9.2-incubating, 0.9.3, 0.9.4 Fix Version/s: 0.10 ...
- 运行基准测试hadoop集群中的问题:org.apache.hadoop.ipc.RemoteException: java.io.IOException: File /benchmarks/TestDFSIO/io_data/test_
在master(即:host2)中执行 hadoop jar hadoop-test-1.1.2.jar DFSCIOTest -write -nrFiles 12 -fileSize 10240 - ...
- Spark启动报错|java.io.FileNotFoundException: File does not exist: hdfs://hadoop101:9000/directory
at org.apache.spark.deploy.history.FsHistoryProvider.<init>(FsHistoryProvider.scala:) at org.a ...
- com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\salary-card\target\salary-card-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\keystore\login_id_rsa 资源未找到
com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\sala ...
- Java—IO流 File类的常用API
File类 1.只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. package cn.test; import java.io.File; import java.io.IOE ...
- 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist
不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句: ./spark-shell --master spark://ma ...
- java io包File类
1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对 ...
- Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist
Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist 一.错误详情 2019-10-17 20:04:49,080 INFO [ ...
- Diagnostics: File file:/private/tmp/spark-d4ebd819-e623-47c3-b008-2a4df8019758/__spark_libs__6824092999244734377.zip does not exist java.io.FileNotFoundException: File file:/private/tmp/spark-d4ebd819
spark伪分布式模式 on-yarn出现一下错误 Diagnostics: File file:/private/tmp/spark-d4ebd819-e623-47c3-b008-2a4df801 ...
随机推荐
- 启动 Eclipse 报错 “An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException”
之前在线升级了Eclipse,由于网络/或者是设置问题,在升级完成后启动Eclipse出线上述错误... 解决方法 1. 删除目录工作目录下面的.project文件夹: 如下图: 2. 关闭Eclip ...
- MySql——事务控制语言(DTL)
什么是事务(控制台只能是内存的操作) 通常,在此之前,我们说,一条语句使用一个分号(;)来结束,并得到执行. 那么我们说,这个“一次性执行”的过程,可以称为“一个事务”. 简单来说,“一条sql语句, ...
- 自己动手实现XXX系列
前记: 最近看了rongjun的一片文章:自己动手实现jdk代理类.按照上面的例子敲完才发现,JDK动态代理 实现底层原来如此简单,只是大量的使用了反射,类编译,类加载一些常规的东西 而且本质也是如实 ...
- Tkinter Bitmaps
Tkinter Bitmaps: 你会使用这个属性显示一个位图.有以下类型的可用位图. 你会使用这个属性显示一个位图.有以下类型的可用位图.: "error" "g ...
- centOS安装openoffice的方法
centOS安装openoffice的方法 分类: centOS 2012-06-15 10:24 2872人阅读 评论(0) 收藏 举报 centos测试 yum install openoffic ...
- 【转】运输层TCP协议详细介绍
TCP是TCP/IP协议族中非常复杂的一个协议.它具有以下特点: 1:面向连接的运输层协议.在使用TCP协议之前,首先需要建立TCP连接.传送数据完毕后,必须释放已经建立的TCP连接. 2:一条TCP ...
- IDEA2017 破解方式
1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ http://ide ...
- 【BZOJ4566】找相同字符【后缀自动机】
题意 给定两个字符串,求两个字符串相同子串的方案数. 分析 那么将字符串s1建SAM,然后对于s2的每个前缀,都在SAM中找出来,并且计数就行. 我一开始的做法是,建一个u和len,顺着s2跑SAM, ...
- 【BZOJ 3261】最大异或和【可持久化字典树】
题意 给出一个长度为n的整数序列,给出m个操作.操作有两种.1,Ax表示在序列结尾增加x.2,Qlrx表示找到一个位置p满足 l<=p<=r,使得a[p] xor a[p+1]xor... ...
- Nginx源码完全注释(3)ngx_list.h / ngx_list.c
列表头文件ngx_list.h #ifndef _NGX_LIST_H_INCLUDED_ #define _NGX_LIST_H_INCLUDED_ #include <ngx_config. ...