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基本操作的更多相关文章

  1. [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 ...

  2. 运行基准测试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 - ...

  3. 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 ...

  4. 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 ...

  5. Java—IO流 File类的常用API

    File类 1.只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. package cn.test; import java.io.File; import java.io.IOE ...

  6. 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist

    不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句:  ./spark-shell  --master spark://ma ...

  7. java io包File类

    1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对 ...

  8. Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist

    Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist 一.错误详情 2019-10-17 20:04:49,080 INFO [ ...

  9. 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 ...

随机推荐

  1. JPA基本介绍以及使用

      JPA即Java Persistence Architecture,Java持久化规范,从EJB2.x版本中原来的实体Bean分离出来的,EJB3.x中不再有实体Bean,而是将实体Bean放到J ...

  2. python中包和模块的使用说明

    python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用. 如果想 ...

  3. 记录关于ubuntu无线上网只能ping通5~7个数据包的问题

    问题是这样的,我的笔记本(ubuntu desktop)连接上wifi后,信号很好,但是上网上不了,ping网关也不通,ping外网仅仅只有当笔记本刚刚连接上wifi的时候能ping通5至6个包,然后 ...

  4. Linux学习笔记 - Shell 控制语句

    if 语句 语法: #!/bin/bash a= b= if [ $a -eq $b ] then echo "a 等于 b" elif [ $a -gt $b ] then ec ...

  5. iBatisNet分布式事务的应用 MS SQL2008。

    所谓分布式事务,即多台数据库服务器在一个事务中运行,因此至少两台及以上的数据库服务器. 一.所有数据库服务器必须配置好MSDTC. 如何配置请大家搜索“MSDTC配置”即可. 大至的配置为: 1.开启 ...

  6. USB驱动程序之USB总线驱动程序学习笔记

    USB总线驱动程序的作用 1. 识别USB设备 1.1 分配地址 1.2 并告诉USB设备(set address) 1.3 发出命令获取描述符 描述符的信息可以在include\linux\usb\ ...

  7. 【UVALive】3695 Distant Galaxy(......)

    题目 传送门:QWQ 分析 好喵啊~~~~ 不会做 正解看蓝书P53吧 代码 #include <cstdio> #include <algorithm> using name ...

  8. oracle打补丁步骤简介

    1.了解opatchopatch是用于维护"个别"补丁的,有人称其为interim path或是one-off patch该命令的存放位置在$ORACLE_HOME下的OPatch ...

  9. Java--神奇的hashcode

    一.Object的HashCode定义 public native int hashCode(); Object类的hashCode方式使用了native修饰也就意味着真正的实现调用的其他语言编写的方 ...

  10. Git(三):Git 使用规范流程

    转:http://www.ruanyifeng.com/blog/2015/08/git-use-process.html 团队开发中,遵循一个合理.清晰的Git使用流程,是非常重要的. 否则,每个人 ...