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. Php处理时间的函数

    1,字符串与时间: 例如 $time = strtotime("2007-3-5"); echo date("Y-m-d H:i:s",$time); 2,当前 ...

  2. kubectl get pods The connection to the server was refused - did you specify the rig

    1   主要是运行这个命令  alias kubectl='kubectl --kubeconfig=/etc/kubernetes/kubelet.conf'问题解决. 同时也用到如下命令: pas ...

  3. An Autofac Lifetime Primer

    Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...

  4. OD 实验(一) - 修改程序标题

    需要修改的程序 把 I love fishc.com 修改为 hello world sch01ar 用 OD 打开程序 在程序入口处开始一直按 F8 运行程序,看看在哪里弹出对话框 运行到该地址的时 ...

  5. 一,我的Android Studio 3.0.1 安装过程

    安装成功于20171231的0:46分. 简要记录我的安装过程如下: 一,安装JDK1.8.X 二,安装ANDROID STUDIO.ZIP 三,运行AS,后按提示下载SDK,NDK,必要时设置一下J ...

  6. Network Real Trace Analysis 2015年12月10日

    了解网络中真实的流量,国内很难找到巨人的肩膀. WAND是新西兰waikato 大学计算机系的研究小组,主要做网络测量,大规模网络流量捕获,网络分析.还做专业的分析软件. libtrace是其开源的分 ...

  7. 针对android&ios yuv旋转、镜像、格式转换、裁剪 算法实现<转>

    http://blog.csdn.net/dangxw_/article/details/50903693

  8. BMP文件结构(转)

    [转自网络] BMP文件存储结构的格式可以在Windows中的WINGDI.h文件中找到定义. BMP文件总体上由4部分组成,分别是位图文件头.位图信息头.调色板和图像数据,如表5-1所示. 表5-1 ...

  9. iOS 上的蓝牙框架 - Core Bluetooth for iOS

    原文: Core Bluetooth for iOS 6 Core Bluetooth 是在iOS5首次引入的,它允许iOS设备可以使用健康,运动,安全,自动化,娱乐,附近等外设数据.在iOS 6 中 ...

  10. Spring <context:annotation-config />讲解

    在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config />这样一条配置,他的作用是向Spring容器注册AutowiredAnnot ...