1、File类简介

创建好:File file=new File("hello.txt"); 后,按住Ctrl键、单击File。会出现File的源代码。

在视图左下角双击“outline”大纲最大化后会出现文件所具有的方法,带有绿色的点,表明方法是公开的,即public 。

 public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("hello.txt");
if (file.exists()) {
System.out.println(file.isFile());//文件可以是文件
System.out.println(file.isDirectory());//也可以是文件夹(路径)
}else {
System.out.println("文件不存在"); //结果是不存在的!

2、文件的创建、删除、重命名。

(1)创建和删除:

public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("hello.txt");
if (file.exists()) { // System.out.println(file.isFile());//文件可以是文件
// System.out.println(file.isDirectory());//也可以是文件夹(路径)
file.delete();//文件删除
System.out.println("文件删除成功");
}else {
System.out.println("文件不存在");
try {
file.createNewFile();
System.out.println("文件已经成功创建");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件无法创建");

(2)重命名:(注意:文件夹结构必须处于同一个分区,文件处于不同的分区,需要使用的是文件的拷贝而不是重命名)

public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("hello.txt");
if (file.exists()) {
File nameto=new File("new hello.txt");//重命名
file.renameTo(nameto);
// System.out.println(file.isFile());//文件可以是文件
// System.out.println(file.isDirectory());//也可以是文件夹(路径)
//file.delete();//文件删除
//System.out.println("文件删除成功");
}else {
System.out.println("文件不存在");
try {
file.createNewFile();
System.out.println("文件已经成功创建");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件无法创建");
}

3、文件夹的创建、删除、重命名。

1

public static void main(String[] args) {
File folder =new File("my new folder");//文件夹的创建
if (folder.mkdirs()) {
System.out.println("文件夹创建完成");
}else {
if (folder.exists()) {
System.out.println("文件夹已经存在不用创建");
}else {
System.out.println("文件夹创建失败");

2、

File folder =new File("my new folder");
File newfolder=new File("my new folder-new");//重命名
if (folder.renameTo(newfolder)) {
System.out.println("重命名成功");
}else {
System.out.println("重命名失败");

3、

File folder =new File("my new folder");
if (folder.delete()) {//删除(只能删空文件夹啊!)
System.out.println("delete suceeded");
}else {
System.out.println("delete failed");

4、文件属性的读取

(1)创建一个文件:在右击工程、new、选untitled text file 、输入内容后、另存为(选中所属工程名)、重命名~

public class ReadFileProperty {

    public static void main(String[] args) {
File file=new File("text.txt");
// 判断文件是否存在
System.out.println("判断文件是否存在"+file.exists());
// 读取文件名称
System.out.println("读取文件名称"+file.getName());
// 读取文件(相对)路径
System.out.println("读取文件路径"+file.getPath());
// 读取文件绝对路径
System.out.println("读取文件绝对路径"+file.getAbsolutePath());
// 获取文件父级路径
System.out.println("获取文件父级路径"+new File(file.getAbsolutePath()).getParent());
// 读取文件大小
System.out.println("读取文件大小"+file.length()+"byte");
System.out.println("读取文件大小"+(float)file.length()/1000+"KB");
// 判断文件是否被隐藏
System.out.println("判断文件是否被隐藏"+file.isHidden());
// 判断文件是否可读
System.out.println("判断文件是否可读"+file.canRead());
// 判断文件是否可写
System.out.println("判断文件是否可写"+file.canWrite());
// 判断文件是否为文件夹
System.out.println("判断文件是否为文件夹"+file.isDirectory());

结果:

判断文件是否存在true

读取文件名称text.txt

读取文件路径text.txt

读取文件绝对路径C:\Documents and Settings\Owner.LENOVO-F94A111E\workspace\ReadFileProperty\text.txt

获取文件父级路径C:\Documents and Settings\Owner.LENOVO-F94A111E\workspace\ReadFileProperty

读取文件大小14byte

读取文件大小0.014KB

判断文件是否被隐藏false

判断文件是否可读true

判断文件是否可写true(如果将文件设置为只读的话 ,那么就是false !)

判断文件是否为文件夹false

1、  文件属性的设置

File file=new File("test.file");   //也是要手动新建一个文件的!
if (file.exists()) {
//将文件设定为可写(前提需要先将它设置为不可写!)
file.setWritable(true);//or false
//将文件设定为可读
file.setReadable(true);
//将文件设定为只读(运行一下语句时需要将上面两个语句注释掉~)
file.setReadOnly();}

2、  遍历文件夹:

public static void main(String[] args) {
// TODO Auto-generated method stub
printFiles(new File("../FileScanner"),1);
}
public static void printFiles(File dir,int tab) {//tab为使层次更清楚
if (dir.isDirectory()) {
File next[]=dir.listFiles();
for (int i = 0; i < next.length; i++) {
for (int j = 0; j < tab; j++) {
System.out.print("|---");//去掉ln。
}
System.out.println(next[i].getName());
if (next[i].isDirectory()) {
printFiles(next[i],tab+1);

结果:

|---.classpath

|---.project

|---.settings

|---|---org.eclipse.jdt.core.prefs

|---bin

|---|---com

|---|---|---jikexueyuan

|---|---|---|---filescan

|---|---|---|---|---main

|---|---|---|---|---|---Scanner.class

|---src

|---|---com

|---|---|---jikexueyuan

|---|---|---|---filescan

|---|---|---|---|---main

|---|---|---|---|---|---Scanner.java

3、  文件的简单读写:

(1)读::

public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("text.txt");
if (file.exists()) {
System.err.println("exist");
try {
FileInputStream fis =new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
BufferedReader br=new BufferedReader(isr); String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
isr.close();
fis.close();//先打开的后关闭,后打开的先关闭
} catch (FileNotFoundException e){
e.printStackTrace();
}catch ( UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();

(2)写:

    File newfile =new File("newtext.txt");
FileOutputStream fos =new FileOutputStream(newfile);
OutputStreamWriter osw =new OutputStreamWriter(fos,"UTF-8");
BufferedWriter bw=new BufferedWriter(osw);
try {
bw.write("长歌行 汉乐府\n");
bw.write("青青园中葵,朝露待日晞。\n");
bw.write("阳春布德泽,万物生光辉。\n");
bw.write("常恐秋节至,焜黄华叶衰。\n");
bw.write("百川东到海,何时复西归?\n");
bw.write("少壮不努力,老大徒伤悲。\n"); bw.close();
osw.close();
fos.close(); System.out.println("写入完成");
} catch (FileNotFoundException e){
e.printStackTrace();
}catch ( UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();

06-Java 本地文件操作的更多相关文章

  1. java常见文件操作

    收集整理的java常见文件操作,方便平时使用: //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if ( ...

  2. 【Egret】实现web页面操作PC端本地文件操作

    Egret 实现web页面操作PC端本地文件操作: http://edn.egret.com/cn/book/page/pid/181 //------------------------------ ...

  3. H5读取本地文件操作

    H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...

  4. HTML5 本地文件操作之FileSystemAPI实例(四)

    目录操作Demo二 1.删除目录 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSyst ...

  5. HTML5 本地文件操作之FileSystemAPI实例(三)

    文件夹操作demo 1.读取根目录文件夹内容 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFi ...

  6. HTML5 本地文件操作之FileSystemAPI实例(二)

    文件操作实例整理二 1.删除文件.复制文件.移动文件 //获取请求权限 window.requestFileSystem = window.requestFileSystem || window.we ...

  7. HTML5 本地文件操作之FileSystemAPI实例(一)

    文件操作实例整理一 1.请求系统配额类型 console.info(window.TEMPORARY); //0 临时 console.info(window.PERSISTENT); //1 持久 ...

  8. HTML5 本地文件操作之FileSystemAPI整理(二)

    一.文件目录操作 1.DirectoryEntry对象 属性: 1.isFile: 操作对象的是否为文件,DirectoryEntry对象固定其值为false 2.isDirectory: 操作对象是 ...

  9. HTML5 本地文件操作之FileSystemAPI整理(一)

    一.请求配额 DeprecatedStorageInfo对象 window.webkitStorageInfo:当使用持久存储模式时需要用到该对象的接口 方法: 1.requestQuota(type ...

  10. Java常用文件操作-2

    上篇文章记录了常用的文件操作,这里记录下通过SSH服务器操作Linux服务器的指定路径下的文件. 这里用到了第三方jar包 jsch-0.1.53.jar, jsch-api 1.删除服务器上指定路径 ...

随机推荐

  1. python中的函数调用绑定,静态方法和类方法

    在C++的类中,有两种函数:普通成员函数和静态成员函数,差别是成员函数通过类实例调用,而静态成员函数通过类名调用.本质上,成员函数在调用的时候会默认把this指针作为第一个参数传入,而静态成员函数不需 ...

  2. IOS 在http请求中使用cookie

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbird.blog.51cto.com/211214/805173 一直以 ...

  3. Interview----链表的倒数第K个元素

    这个题虽然简单,但是一定要细心,bug-free 能力很重要. 分析: 如果不知道链表的长度,可以采用双指针的方法,让一个指针先走 k 步,然后两个指针同时走, 前面的指针变成 NULL时, 第一个指 ...

  4. 51 nod 机器人走方格

    从一个长方形的方格的右上角 走到 左下角 , 问一共有多少种不同的路线可以达到 . #include<stdio.h> #include<string.h> #include& ...

  5. Manacher算法 , 实例 详解 . NYOJ 最长回文

    51 Nod http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 Manacher 算法 定义数组 p[i]表示以i为 ...

  6. C#泛型(二)

    <1>.泛型方法 以前文章说到用一个泛型类 SortHelper 来做一个冒泡排序的处理,下面回顾一下之前的代码: public class SortHelper<T> whe ...

  7. Best Sequence_DFS&&KMp

    Description The twenty-first century is a biology-technology developing century. One of the most att ...

  8. 微信Accesstoken通过xml文件方式保存

    //获取accessToken public static AccessToken GetAccessToken() { string AppID = JobBase.GetConfParamValu ...

  9. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  10. PHP 调用oracle存储过程

    $orderdate = strtotime($this->input->get('orderdate')); $today = strtotime(date('Y-m-d',time() ...