Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
beginnersbook-zh
提交
061484e8
B
beginnersbook-zh
项目概览
OpenDocCN
/
beginnersbook-zh
9 个月 前同步成功
通知
0
Star
107
Fork
30
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beginnersbook-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
061484e8
编写于
10月 13, 2019
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
2019-10-13 10:43:40
上级
33f30f50
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
64 addition
and
61 deletion
+64
-61
docs/java-io/82.md
docs/java-io/82.md
+5
-5
docs/java-io/83.md
docs/java-io/83.md
+5
-5
docs/java-io/84.md
docs/java-io/84.md
+5
-5
docs/java-io/85.md
docs/java-io/85.md
+11
-11
docs/java-io/86.md
docs/java-io/86.md
+3
-3
docs/java-io/87.md
docs/java-io/87.md
+2
-2
docs/java-io/88.md
docs/java-io/88.md
+7
-7
docs/java-io/89.md
docs/java-io/89.md
+4
-4
docs/java-io/90.md
docs/java-io/90.md
+5
-5
docs/java-io/91.md
docs/java-io/91.md
+12
-9
docs/java-io/SUMMARY.md
docs/java-io/SUMMARY.md
+5
-5
未找到文件。
docs/java-io/82.md
浏览文件 @
061484e8
...
@@ -2,14 +2,14 @@
...
@@ -2,14 +2,14 @@
> 原文: [https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/](https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/)
> 原文: [https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/](https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/)
在本教程中,我们将了解如何使用
`createNewFile()`
方法在 Java 中创建文件。如果文件在指定位置不存在并且返回
true,则此方法创建一个空文件。如果文件已存在,则此方法返回 false
。它抛出:
在本教程中,我们将了解如何使用
`createNewFile()`
方法在 Java 中创建文件。如果文件在指定位置不存在并且返回
`true`
,则此方法创建一个空文件。如果文件已存在,则此方法返回
`false`
。它抛出:
[
`IOException`
](
https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html
"class in java.io"
)
- 如果在创建文件期间发生输入/输出错误。
[
`IOException`
](
https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html
)
- 如果在创建文件期间发生输入/输出错误。
[
`SecurityException`
](
https://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html
"class in java.lang"
)
- 如果存在安全管理器且其
[
`SecurityManager.checkWrite(java.lang.String)`
](
https://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkWrite(java.lang.String
)
)
方法拒绝对该文件的写访问。
[
`SecurityException`
](
https://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html
)
- 如果存在安全管理器且其
[
`SecurityManager.checkWrite(java.lang.String)`
](
https://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkWrite(java.lang.String
)
)
方法拒绝对该文件的写访问。
**完整代码:**
**完整代码:**
下面的代码将在 C 盘中创建一个名为
“newfile.txt”的 txt
文件。您可以更改以下代码中的路径,以便在不同目录或不同驱动器中创建文件。
下面的代码将在 C 盘中创建一个名为
`newfile.txt`
的
`txt`
文件。您可以更改以下代码中的路径,以便在不同目录或不同驱动器中创建文件。
```
java
```
java
package
beginnersbook.com
;
package
beginnersbook.com
;
...
@@ -43,4 +43,4 @@ public class CreateFileDemo
...
@@ -43,4 +43,4 @@ public class CreateFileDemo
#### 参考:
#### 参考:
[
Javadoc:createNewFile()
](
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createNewFile(
)
"createNewFile() method")
[
Javadoc:`createNewFile()`
](
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createNewFile(
)
)
\ No newline at end of file
\ No newline at end of file
docs/java-io/83.md
浏览文件 @
061484e8
# 如何在 Java 中读取文件 -
BufferedInputStream
# 如何在 Java 中读取文件 -
`BufferedInputStream`
> 原文: [https://beginnersbook.com/2014/01/how-to-read-file-in-java-bufferedinputstream/](https://beginnersbook.com/2014/01/how-to-read-file-in-java-bufferedinputstream/)
> 原文: [https://beginnersbook.com/2014/01/how-to-read-file-in-java-bufferedinputstream/](https://beginnersbook.com/2014/01/how-to-read-file-in-java-bufferedinputstream/)
在这个例子中,我们将看到如何使用
`FileInputStream`
和
`BufferedInputStream`
在 Java 中读取文件。以下是我们在下面的代码中采取的详细步骤:
在这个例子中,我们将看到如何使用
`FileInputStream`
和
`BufferedInputStream`
在 Java 中读取文件。以下是我们在下面的代码中采取的详细步骤:
1)通过在创建文件对象期间提供文件的完整路径(我们将读取)来创建
File
实例。
1)通过在创建文件对象期间提供文件的完整路径(我们将读取)来创建
`File`
实例。
2)将文件实例传递给
[
FileInputStream
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#FileInputStream(java.io.File
)
) ,它打开与实际文件的连接,该文件由文件系统中的 File
对象文件命名。
2)将文件实例传递给
[
`FileInputStream`
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#FileInputStream(java.io.File
)
),它打开与实际文件的连接,该文件由文件系统中的
`File`
对象文件命名。
3)将
`FileInputStream`
实例传递给
[
BufferedInputStream
](
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html#BufferedInputStream(java.io.InputStream
)
) ,它创建
`BufferedInputStream`
并保存其参数,即输入流,供以后使用。创建内部缓冲区数组并将其存储在 buf
中,使用该数组,读取操作可提供良好的性能,因为内容在缓冲区中很容易获得。
3)将
`FileInputStream`
实例传递给
[
`BufferedInputStream`
](
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html#BufferedInputStream(java.io.InputStream
)
) ,它创建
`BufferedInputStream`
并保存其参数,即输入流,供以后使用。创建内部缓冲区数组并将其存储在
`buf`
中,使用该数组,读取操作可提供良好的性能,因为内容在缓冲区中很容易获得。
4)用于循环读取文件。方法
[
available()
](
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html#available(
)
)用于检查文件的结尾,因为当指针到达文件末尾时返回 0。使用
`FileInputStream`
的
[
read()
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read(
)
)方法读取文件内容。
4)用于循环读取文件。方法
[
`available()`
](
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html#available(
)
)用于检查文件的结尾,因为当指针到达文件末尾时返回 0。使用
`FileInputStream`
的
[
`read()`
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read(
)
)方法读取文件内容。
```
java
```
java
package
beginnersbook.com
;
package
beginnersbook.com
;
...
...
docs/java-io/84.md
浏览文件 @
061484e8
# 如何
使用 FileOutputStream 写入 java 中的
文件
# 如何
在 java 中使用`FileOutputStream`写入
文件
> 原文: [https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/](https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/)
> 原文: [https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/](https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/)
之前我们看过如何
[
在 Java
](
https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/
)
中创建一个文件。在本教程中,我们将看到如何使用
`FileOutputStream`
在 java 中写入文件。我们将使用
`FileOutputStream`
的
[
write()
方法
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#write(byte[]
)
)将内容写入指定的文件。这是
`write()`
方法的签名。
之前我们看过如何
[
在 Java
](
https://beginnersbook.com/2014/01/how-to-create-a-file-in-java/
)
中创建一个文件。在本教程中,我们将看到如何使用
`FileOutputStream`
在 java 中写入文件。我们将使用
`FileOutputStream`
的
[
`write()`
方法
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html#write(byte[]
)
)将内容写入指定的文件。这是
`write()`
方法的签名。
```
java
```
java
public
void
write
(
byte
[]
b
)
throws
IOException
public
void
write
(
byte
[]
b
)
throws
IOException
...
@@ -12,7 +12,7 @@ public void write(byte[] b) throws IOException
...
@@ -12,7 +12,7 @@ public void write(byte[] b) throws IOException
## 完整代码:写入文件
## 完整代码:写入文件
在下面的例子中,我们将
`String`
写入文件。要将
`String`
转换为字节数组,我们使用
[
String 类
](
https://beginnersbook.com/2013/12/java-strings/
)
的
[
getBytes()
方法
](
https://beginnersbook.com/2013/12/java-string-getbytes-method-example/
)
。
在下面的例子中,我们将
`String`
写入文件。要将
`String`
转换为字节数组,我们使用
[
`String`类
](
https://beginnersbook.com/2013/12/java-strings/
)
的
[
`getBytes()`
方法
](
https://beginnersbook.com/2013/12/java-string-getbytes-method-example/
)
。
```
java
```
java
import
java.io.File
;
import
java.io.File
;
...
@@ -71,6 +71,6 @@ public class WriteFileDemo {
...
@@ -71,6 +71,6 @@ public class WriteFileDemo {
File
Written
Successfully
File
Written
Successfully
```
```
#### 参考
文献
:
#### 参考:
[
FileOutputStream - JavaDoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
)
[
`FileOutputStream` - JavaDoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
)
\ No newline at end of file
\ No newline at end of file
docs/java-io/85.md
浏览文件 @
061484e8
# 使用
BufferedWriter,PrintWriter,FileWriter
附加到 java 中的文件
# 使用
`BufferedWriter`,`PrintWriter`,`FileWriter`
附加到 java 中的文件
> 原文: [https://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/](https://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/)
> 原文: [https://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/](https://beginnersbook.com/2014/01/how-to-append-to-a-file-in-java/)
...
@@ -8,7 +8,7 @@
...
@@ -8,7 +8,7 @@
2)使用
`PrintWriter`
:这是将内容附加到文件的最佳方式之一。无论你使用
`PrintWriter`
对象写什么都会附加到文件中。
2)使用
`PrintWriter`
:这是将内容附加到文件的最佳方式之一。无论你使用
`PrintWriter`
对象写什么都会附加到文件中。
## 1)使用
FileWriter 和 BufferedWriter 将内容附加到 File
## 1)使用
`FileWriter`和`BufferedWriter`将内容附加到`File`
```
java
```
java
import
java.io.File
;
import
java.io.File
;
...
@@ -57,7 +57,7 @@ class AppendFileDemo
...
@@ -57,7 +57,7 @@ class AppendFileDemo
Data
successfully
appended
at
the
end
of
file
Data
successfully
appended
at
the
end
of
file
```
```
让我们说
myfile.txt
的内容是:
让我们说
`myfile.txt`
的内容是:
```
java
```
java
This
is
the
already
present
content
of
my
file
This
is
the
already
present
content
of
my
file
...
@@ -70,7 +70,7 @@ This is the already present content of my fileThis is my content which
...
@@ -70,7 +70,7 @@ This is the already present content of my fileThis is my content which
would
be
appended
at
the
end
of
the
specified
file
would
be
appended
at
the
end
of
the
specified
file
```
```
## 2)使用
PrintWriter 将内容附加到 File
## 2)使用
`PrintWriter`将内容附加到`File`
`PrintWriter`
为您提供更大的灵活性。使用此功能,您可以轻松格式化要附加到
`File`
的内容。
`PrintWriter`
为您提供更大的灵活性。使用此功能,您可以轻松格式化要附加到
`File`
的内容。
...
@@ -113,19 +113,19 @@ class AppendFileDemo2
...
@@ -113,19 +113,19 @@ class AppendFileDemo2
}
}
```
```
Output:
输出:
```
java
```
java
Data
successfully
appended
at
the
end
of
file
Data
successfully
appended
at
the
end
of
file
```
```
Lets say myfile.txt content was:
让我们说
`myfile.txt`
的内容是:
```
java
```
java
This
is
the
already
present
content
of
my
file
This
is
the
already
present
content
of
my
file
```
```
After running the above program the content would be:
运行上述程序后,内容将是:
```
java
```
java
This
is
the
already
present
content
of
my
file
This
is
the
already
present
content
of
my
file
...
@@ -134,8 +134,8 @@ This is the second line
...
@@ -134,8 +134,8 @@ This is the second line
This
is
third
line
This
is
third
line
```
```
#### 参考
文献
:
#### 参考:
*
[
FileWriter - JavaDoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
"FileWriter"
)
*
[
`FileWriter` - JavaDoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
"FileWriter"
)
*
[
BufferedWriter - JavaDoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html
)
*
[
`BufferedWriter` - JavaDoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html
)
*
[
PrintWriter - Javadoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
"PrintWriter"
)
*
[
`PrintWriter` - Javadoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html
"PrintWriter"
)
\ No newline at end of file
\ No newline at end of file
docs/java-io/86.md
浏览文件 @
061484e8
# 如何在 Java 中删除文件 -
delete()
方法
# 如何在 Java 中删除文件 -
`delete()`
方法
> 原文: [https://beginnersbook.com/2014/01/how-to-delete-file-in-java-delete-method/](https://beginnersbook.com/2014/01/how-to-delete-file-in-java-delete-method/)
> 原文: [https://beginnersbook.com/2014/01/how-to-delete-file-in-java-delete-method/](https://beginnersbook.com/2014/01/how-to-delete-file-in-java-delete-method/)
...
@@ -8,7 +8,7 @@
...
@@ -8,7 +8,7 @@
public
boolean
delete
()
public
boolean
delete
()
```
```
如果指定的
`File`
成功删除,则此方法返回
true,否则返回 false
。
如果指定的
`File`
成功删除,则此方法返回
`true`
,否则返回
`false`
。
**这是完整的代码:**
**这是完整的代码:**
...
@@ -45,4 +45,4 @@ myfile.txt is deleted!
...
@@ -45,4 +45,4 @@ myfile.txt is deleted!
**参考:**
**参考:**
[
删除() - Javadoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete(
)
)
[
`delete()` - Javadoc
](
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete(
)
)
\ No newline at end of file
\ No newline at end of file
docs/java-io/87.md
浏览文件 @
061484e8
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
> 原文: [https://beginnersbook.com/2014/07/how-to-compress-a-file-in-gzip-format/](https://beginnersbook.com/2014/07/how-to-compress-a-file-in-gzip-format/)
> 原文: [https://beginnersbook.com/2014/07/how-to-compress-a-file-in-gzip-format/](https://beginnersbook.com/2014/07/how-to-compress-a-file-in-gzip-format/)
以下代码将指定的文件压缩为 GZip 格式。在下面的示例中,我们在
“Java”文件夹下的 B 驱动器
中有一个文本文件,我们正在压缩并生成同一文件夹中的 GZip 文件。
以下代码将指定的文件压缩为 GZip 格式。在下面的示例中,我们在
`B`
驱动器的
`Java`
文件夹下
中有一个文本文件,我们正在压缩并生成同一文件夹中的 GZip 文件。
```
java
```
java
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
...
@@ -70,4 +70,4 @@ File Compressed!!
...
@@ -70,4 +70,4 @@ File Compressed!!
**参考:**
**参考:**
[
GZIPOutputStream javadoc
](
https://docs.oracle.com/javase/7/docs/api/java/util/zip/GZIPOutputStream.html
)
[
`GZIPOutputStream` javadoc
](
https://docs.oracle.com/javase/7/docs/api/java/util/zip/GZIPOutputStream.html
)
\ No newline at end of file
\ No newline at end of file
docs/java-io/88.md
浏览文件 @
061484e8
...
@@ -2,11 +2,11 @@
...
@@ -2,11 +2,11 @@
> 原文: [https://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/](https://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/)
> 原文: [https://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/](https://beginnersbook.com/2014/05/how-to-copy-a-file-to-another-file-in-java/)
在本教程中,我们将了解如何将一个文件的内容复制到 java 中的另一个文件中。为了复制文件,首先我们可以
[
使用
](
https://beginnersbook.com/2014/01/how-to-read-file-in-java-bufferedinputstream/
)
[
FileInputStream
]
(https://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html) 读取文件然后我们可以
[
使用
](
https://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/
)
[
FileOutputStream 将读取的内容写入输出文件
]
(https://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html)
。
在本教程中,我们将了解如何将一个文件的内容复制到 java 中的另一个文件中。为了复制文件,首先我们可以
使用
[
`FileInputStream`
](
https://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html
)
读取文件然后我们可以使用
[
`FileOutputStream`将读取的内容写入输出文件
](
https://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream.html
)
。
### 例
### 例
下面的代码会将
“MyInputFile.txt”的内容复制到“MyOutputFile.txt”文件中。如果“MyOutputFile.txt”
不存在,则程序将首先创建文件,然后复制内容。
下面的代码会将
`MyInputFile.txt`
的内容复制到
`MyOutputFile.txt`
文件中。如果
`MyOutputFile.txt`
不存在,则程序将首先创建文件,然后复制内容。
```
java
```
java
import
java.io.File
;
import
java.io.File
;
...
@@ -59,15 +59,15 @@ File copied successfully!!
...
@@ -59,15 +59,15 @@ File copied successfully!!
上述程序中使用的方法是:
上述程序中使用的方法是:
####
read()
方法
####
`read()`
方法
```
java
```
java
public
int
read
(
byte
[]
b
)
throws
IOException
public
int
read
(
byte
[]
b
)
throws
IOException
```
```
将此输入流中的
b.length 个字节数据读入一个字节数组。此方法将阻止,直到某些输入可用。它返回读入缓冲区的总字节数,如果没有更多数据,则返回-1,因为已到达文件末尾。为了使这个方法在我们的程序中工作,我们创建了一个字节数组“buffer”并将输入文件的内容读取到相同的内容。由于此方法抛出 IOException,因此我们将“读取文件”代码放在 try-catch
块中以处理异常。
将此输入流中的
`b.length`
个字节数据读入一个字节数组。此方法将阻止,直到某些输入可用。它返回读入缓冲区的总字节数,如果没有更多数据,则返回 -1,因为已到达文件末尾。为了使这个方法在我们的程序中工作,我们创建了一个字节数组
`buffer`
并将输入文件的内容读取到相同的内容。由于此方法抛出
`IOException`
,因此我们将“读取文件”代码放在
`try-catch`
块中以处理异常。
####
write()
方法
####
`write()`
方法
```
java
```
java
public
void
write
(
byte
[]
b
,
public
void
write
(
byte
[]
b
,
...
@@ -76,11 +76,11 @@ public void write(byte[] b,
...
@@ -76,11 +76,11 @@ public void write(byte[] b,
throws
IOException
throws
IOException
```
```
将从
offset off
开始的指定字节数组的长度字节写入此文件输出流。
将从
偏移
`off`
开始的指定字节数组的长度字节写入此文件输出流。
**调整:**
**调整:**
如果输入和输出文件不在同一个驱动器中,则可以在创建文件对象时指定驱动器。例如,如果您的输入文件在
C 盘中并且输出文件在 D
盘中,那么您可以创建如下文件对象:
如果输入和输出文件不在同一个驱动器中,则可以在创建文件对象时指定驱动器。例如,如果您的输入文件在
`C`
盘中并且输出文件在
`D`
盘中,那么您可以创建如下文件对象:
```
java
```
java
File
infile
=
new
File
(
"C:\\MyInputFile.txt"
);
File
infile
=
new
File
(
"C:\\MyInputFile.txt"
);
...
...
docs/java-io/89.md
浏览文件 @
061484e8
...
@@ -2,17 +2,17 @@
...
@@ -2,17 +2,17 @@
> 原文: [https://beginnersbook.com/2014/05/how-to-get-the-last-modified-date-of-a-file-in-java/](https://beginnersbook.com/2014/05/how-to-get-the-last-modified-date-of-a-file-in-java/)
> 原文: [https://beginnersbook.com/2014/05/how-to-get-the-last-modified-date-of-a-file-in-java/](https://beginnersbook.com/2014/05/how-to-get-the-last-modified-date-of-a-file-in-java/)
在这里,我们将学习如何在 java 中获取文件的最后修改日期。为了做到这一点,我们可以使用
File 类的
[
lastModified()
方法
](
https://docs.oracle.com/javase/6/docs/api/java/io/File.html#lastModified(
)
)。以下是此方法的签名。
在这里,我们将学习如何在 java 中获取文件的最后修改日期。为了做到这一点,我们可以使用
`File`
类的
[
`lastModified()`
方法
](
https://docs.oracle.com/javase/6/docs/api/java/io/File.html#lastModified(
)
)。以下是此方法的签名。
```
java
```
java
public
long
lastModified
()
public
long
lastModified
()
```
```
它返回上次修改此抽象路径名表示的文件的时间。此方法返回的值以毫秒为单位,因此为了使其可读,我们可以使用
SimpleDateFormat
格式化输出。
它返回上次修改此抽象路径名表示的文件的时间。此方法返回的值以毫秒为单位,因此为了使其可读,我们可以使用
`SimpleDateFormat`
格式化输出。
#### 完整代码:
#### 完整代码:
这里我们获取文件
“Myfile.txt”的最后修改日期,该日期存在于驱动器“C”中。由于方法返回的值不可读,我们使用 SimpleDateFormat 类的 format()
方法对其进行格式化。
这里我们获取文件
`Myfile.txt`
的最后修改日期,该日期存在于驱动器
`C`
中。由于方法返回的值不可读,我们使用
`SimpleDateFormat`
类的
`format()`
方法对其进行格式化。
```
java
```
java
import
java.io.File
;
import
java.io.File
;
...
@@ -48,4 +48,4 @@ System.out.println("Last Modified Date: " + sdf2.format(file.lastModified()));
...
@@ -48,4 +48,4 @@ System.out.println("Last Modified Date: " + sdf2.format(file.lastModified()));
Last
Modified
Date:
01
-
03
-
14
22
:
41
PM
Last
Modified
Date:
01
-
03
-
14
22
:
41
PM
```
```
您可以使用其他几种模式来获得所需形式的输出。要阅读有关日期格式的更多信息,请参阅
[
SimpleDateFormat javadoc
](
https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
)
。
您可以使用其他几种模式来获得所需形式的输出。要阅读有关日期格式的更多信息,请参阅
[
`SimpleDateFormat` javadoc
](
https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
)
。
\ No newline at end of file
\ No newline at end of file
docs/java-io/90.md
浏览文件 @
061484e8
...
@@ -12,7 +12,7 @@
...
@@ -12,7 +12,7 @@
#### 1)将文件属性更改为只读
#### 1)将文件属性更改为只读
要使文件只读,我们可以使用
File 类的
`setReadOnly()`
方法。它返回一个布尔值,我们可以进一步验证操作是否成功,就像我在下面的程序中一样。正如您在下面的程序中看到的那样,我将文件属性更改为只读取我的计算机“C 盘”中存在的文件“Myfile.txt”
。
要使文件只读,我们可以使用
`File`
类的
`setReadOnly()`
方法。它返回一个布尔值,我们可以进一步验证操作是否成功,就像我在下面的程序中一样。正如您在下面的程序中看到的那样,我将文件属性更改为只读取我的计算机“C 盘”中存在的文件
`Myfile.txt`
。
```
java
```
java
import
java.io.File
;
import
java.io.File
;
...
@@ -46,7 +46,7 @@ File successfully converted to Read only mode!!
...
@@ -46,7 +46,7 @@ File successfully converted to Read only mode!!
#### 2)检查文件是可写还是只读
#### 2)检查文件是可写还是只读
为了检查文件属性,我们可以使用文件类的
`canWrite()`
方法。如果文件是可写的,则此方法返回
true,否则返回 false。当我在已经设置为仅在之前的程序中读取的文件“Myfile.txt”上执行操作时,我将输出为“File is read only”
。
为了检查文件属性,我们可以使用文件类的
`canWrite()`
方法。如果文件是可写的,则此方法返回
`true`
,否则返回
`false`
。当我在已经设置为仅在之前的程序中读取的文件
`Myfile.txt`
上执行操作时,我将输出为
`"File is read only"`
。
```
java
```
java
import
java.io.File
;
import
java.io.File
;
...
@@ -69,7 +69,7 @@ public class CheckAttributes
...
@@ -69,7 +69,7 @@ public class CheckAttributes
}
}
```
```
Output:
输出:
```
java
```
java
File
is
read
only
.
File
is
read
only
.
...
@@ -105,7 +105,7 @@ public class MakeWritable
...
@@ -105,7 +105,7 @@ public class MakeWritable
}
}
```
```
Output:
输出:
```
java
```
java
File
is
writable
.
File
is
writable
.
...
@@ -113,4 +113,4 @@ File is writable.
...
@@ -113,4 +113,4 @@ File is writable.
#### 参考:
#### 参考:
[
档案 Javadoc
](
https://docs.oracle.com/javase/6/docs/api/java/io/File.html
"javadoc"
)
[
`File` Javadoc
](
https://docs.oracle.com/javase/6/docs/api/java/io/File.html
"javadoc"
)
\ No newline at end of file
\ No newline at end of file
docs/java-io/91.md
浏览文件 @
061484e8
# 如何
检查文件是否隐藏在 Java 中
# 如何
在 Java 中检查文件是否隐藏
> 原文: [https://beginnersbook.com/2015/01/how-to-check-if-a-file-is-hidden-in-java/](https://beginnersbook.com/2015/01/how-to-check-if-a-file-is-hidden-in-java/)
> 原文: [https://beginnersbook.com/2015/01/how-to-check-if-a-file-is-hidden-in-java/](https://beginnersbook.com/2015/01/how-to-check-if-a-file-is-hidden-in-java/)
在本教程中,我们将学习如何编写程序来检查特定文件是否被隐藏。我们将使用
File 类的 isHidden()方法来执行此检查。此方法返回布尔值(true 或 false),如果文件被隐藏,则此方法返回 true,否则返回 false
值。
在本教程中,我们将学习如何编写程序来检查特定文件是否被隐藏。我们将使用
`File`
类的
`isHidden()`
方法来执行此检查。此方法返回布尔值(
`true`
或
`false`
),如果文件被隐藏,则此方法返回
`true`
,否则返回
`false`
值。
#### 这是完整的代码:
#### 这是完整的代码:
...
@@ -28,22 +28,25 @@ public class HiddenPropertyCheck
...
@@ -28,22 +28,25 @@ public class HiddenPropertyCheck
```
```
#### 关于 [javadoc](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 的
isHidden()
方法的更多细节:
#### 关于 [javadoc](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html) 的
`isHidden()`
方法的更多细节:
**public static boolean isHidden(Path path)throws IOException**
```
java
public
static
boolean
isHidden
(
Path
path
)
throws
IOException
```
判断文件是否被视为隐藏。隐藏的确切定义是平台或提供者依赖。例如,在 UNIX 上,如果文件的名称以句点字符(
'。'
)开头,则认为该文件是隐藏的。在 Windows 上,如果文件不是目录并且设置了 DOS 隐藏属性,则该文件被视为隐藏。
判断文件是否被视为隐藏。隐藏的确切定义是平台或提供者依赖。例如,在 UNIX 上,如果文件的名称以句点字符(
`'.'`
)开头,则认为该文件是隐藏的。在 Windows 上,如果文件不是目录并且设置了 DOS 隐藏属性,则该文件被视为隐藏。
根据实现方式,此方法可能需要访问文件系统以确定文件是否被视为隐藏。
根据实现方式,此方法可能需要访问文件系统以确定文件是否被视为隐藏。
**参数:**
**参数:**
path
- 要测试的文件的路径
`path`
- 要测试的文件的路径
**返回:**
**返回:**
如果文件被视为隐藏,则返回
true
如果文件被视为隐藏,则返回
`true`
**抛出:**
**抛出:**
IOException - 如果发生 I / O 错误
`IOException`
- 如果发生 I/O 错误
SecurityException - 如果是默认提供程序,并且安装了安全管理器,则调用 checkRead 方法以检查对文件。
\ No newline at end of file
`SecurityException`
- 如果是默认提供程序,并且安装了安全管理器,则调用
`checkRead`
方法以检查对文件。
\ No newline at end of file
docs/java-io/SUMMARY.md
浏览文件 @
061484e8
+
[
如何在 Java 中创建文件
](
82.md
)
+
[
如何在 Java 中创建文件
](
82.md
)
+
[
如何在 Java 中读取文件 -
BufferedInputStream
](
83.md
)
+
[
如何在 Java 中读取文件 -
`BufferedInputStream`
](
83.md
)
+
[
如何
使用 FileOutputStream 写入 java 中的
文件
](
84.md
)
+
[
如何
在 java 中使用`FileOutputStream`写入
文件
](
84.md
)
+
[
使用
BufferedWriter,PrintWriter,FileWriter
附加到 java 中的文件
](
85.md
)
+
[
使用
`BufferedWriter`,`PrintWriter`,`FileWriter`
附加到 java 中的文件
](
85.md
)
+
[
如何在 Java 中删除文件 -
delete()
方法
](
86.md
)
+
[
如何在 Java 中删除文件 -
`delete()`
方法
](
86.md
)
+
[
如何以 GZIP 格式压缩文件
](
87.md
)
+
[
如何以 GZIP 格式压缩文件
](
87.md
)
+
[
如何使用 Java 将文件复制到另一个文件
](
88.md
)
+
[
如何使用 Java 将文件复制到另一个文件
](
88.md
)
+
[
如何在 java 中获取文件的最后修改日期
](
89.md
)
+
[
如何在 java 中获取文件的最后修改日期
](
89.md
)
+
[
如何在 Java 中创建只读文件
](
90.md
)
+
[
如何在 Java 中创建只读文件
](
90.md
)
+
[
如何检查文件是否隐藏在 Java 中
](
91.md
)
+
[
如何在 Java 中检查文件是否隐藏
](
91.md
)
\ No newline at end of file
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录