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. orzdba_monitor.sh脚本使用

    1.orzdba_monitor.sh脚本使用 ./orzdba_monitor.sh 主要是用nohup同时在后台调用orzdba,启动下面三个命令 [root@node02 scripts]# p ...

  2. Linux 文件描述符详解

    Overview 了解Linux怎样处理输入和输出是非常重要的.一旦我们了解其原理以后,我们就可以正确熟练地使用脚本把内容输出到正确的位置.同样我们也可以更好地理解输入重定向和输出重定向. Linux ...

  3. 【UVA】201 Squares(模拟)

    题目 题目     分析 记录一下再预处理一下.     代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf ...

  4. View.findViewById()和Activity.findViewById()区别

    在网上看见View.findViewById() 和 Activity.findViewById()执行效率不一样 使用Activity.findViewById()如: TextView tv_in ...

  5. openStack nova nova valid hosts 优化

    scheduler_default_filters=AllHostsFilterallow_resize_to_same_host=Trueallow_migrate_to_same_host=Tru ...

  6. Form Data 和 Request Payload 区别

    Form Data 和 Request Payload 区别 如果请求头里设置Content-Type: application/x-www-form-urlencoded,那么这个请求被认为是表单请 ...

  7. RHCE7 学习里程-3基本命令

    一.centos7  基本命令 #创建文件 touch a.b #创建文件夹 mkdir abc #删除文件 rm -f a.b #删除空文件夹 rm -rf abc #重命名文件 mv 源文件 新文 ...

  8. IOSerialize(序列化)

    在讲序列化和反序列化之前,先来阐述文件夹/文件 检查.新增.复制.移动.删除, Directory和DirectotyInfo这两个特性主要是对文件夹进行操作 首先检测文件夹是否存在 if (!Dir ...

  9. 浅谈Job&JobDetail

    JobDetai重要属性:

  10. 利用UUID 随机生成8位短号

    //获得8位短号 public static String[] chars = new String[] { "a", "b", "c", ...