FileStream类
FIleOutputStream类
该类是OutputStream类的子类,用来处理以文件作为数据输出的数据流。创建一个文件流对象也有俩种方法。
- 方法一:
File f = new File("./src/1.txt"); //相对路径,./表示当前路径
FileOutputStream out = new FileOutputStream(f);
- 方法二:
FileOutputStream out = new FileOutputStream("./src/1.txt");
在向文件中写入数据时,若文件已存在,则覆盖存在的文件内容,若文件不存在,则新建该文件写入数据。在数据输出的操作中,应当捕捉异常。
向文件写入数据
import java.io.*;
class fileWriteInfo{
void writeInfo() throws IOException {
byte b[] = new byte[16];
FileOutputStream fout = new FileOutputStream("./src/1.txt");
try{
System.out.print("Enter 16 chars: ");
for(int i = 0; i < 16; i++) // Reading 16 bytes from KeyBoard
b[i] = (byte)System.in.read();
fout.write(b); // writing array's content in File one time
}catch(IOException e){
System.out.println("File IOException!");
}finally{
fout.close();
}
}
}
public class FileWriteTest {
public static void main(String[] arg){
fileWriteInfo obj = new fileWriteInfo();
try{
obj.writeInfo();
}catch(IOException e){
System.out.println("File not Found: " + e);
e.printStackTrace();
}
}
}
采用
throws
指明IOEXception
(输入输出异常)时,这是因为在生成文件输出流对象时,执行new FileOutputStream("./src/1.txt")
语句可能会出现异常。
运行实例:
FileInputStream类
该类是InputStream类的子类,用来处理以文件作为数据输入的数据流。创建一个文件流对象也有俩种方法。
- 方法一:
File fin = new File("./src/1.txt");
FileInputStream in = new FileInputStream(fin);
- 方法二:
FileInputStream in = new FileInputStream("./src/1.txt");
从文件中读取
import java.io.*;
class readFile{
void readInfo() throws IOException{
int size = 0;
FileInputStream fin = new FileInputStream("./src/1.txt");
try{
size = fin.available();
System.out.println("file's size: " + size);
System.out.println("Read the first 1/4: ");
byte b_1[] = new byte[size/4];
fin.read(b_1);
String str_1 = new String(b_1);
System.out.println("The first 1/4 is: " + str_1);
System.out.println();
System.out.println("Skip the next 1/2 of the file.");
fin.skip(size/2);
System.out.println("Still available is: " + fin.available());
System.out.println();
System.out.println("Read the next 1/2: ");
byte b_2[] = new byte[fin.available()];
fin.read(b_2);
String str_2 = new String(b_2);
System.out.println("The last is: " + str_2);
}catch(FileNotFoundException e){
System.out.println("File Not Found: " + e);
throw e;
}finally{
fin.close();
}
}
}
public class FileReadTest {
public static void main(String[] arg){
readFile obj = new readFile();
try{
obj.readInfo();
}catch(Exception e){
System.out.println("File Not Found: " + e);
e.printStackTrace();
}
}
}
注意抛出
FileNotFoundException
异常,防止文件没找到,不存在。
运行实例:
FileAttribute
不同操作系统中的文件系统对于文件属性的支持不同,为了避免不同平台读取文件属性困难的问题,Java对文件属性进行了抽象,采用文件属性视图的概念,使得用户更加方便取得属性。
- java.nio.file.attribute.BasicFileAttributeView:用于获取不同文件系统中的通用属性,如最后修改时间、最后访问时间、创建时间等。
- java.nio.file.attribute.DosFileAttributeView:用于获取或修改文件Dos属性,如文件是否是系统文件、是否可读、是否隐藏等。
读取文件属性
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.util.Date;
public class FilleAttributeTest {
public static void main(String[] arg) throws Exception{
Path path = Paths.get("./src/1.txt"); // Relative path
BasicFileAttributeView basicView = Files.getFileAttributeView(path, BasicFileAttributeView.class);
BasicFileAttributes basicFile = basicView.readAttributes();
System.out.println("File's created time: "+ new Date(basicFile.creationTime().toMillis()));
System.out.println("File's size: "+ basicFile.size());
DosFileAttributeView dosView = Files.getFileAttributeView(path, DosFileAttributeView.class);
dosView.setHidden(true); //Hide this File in System.
dosView.setReadOnly(true); // Set attribute of ReadOnly.
System.out.println("File's attribute of ReadOnly: " + dosView.readAttributes().isReadOnly());
}
}
运行实例:
我们可以看见,文件系统里面已经没有了该文件,隐藏并且设为只读。
当我们修改文件时,就会弹出这样的窗口:

点击ok键就可以清除只读属性。
FileRename
只是作为抛砖引玉的作用,因为情况并不是每个人都一样。
下面这个重命名文件名的方式只适用于无序文件,也就是对文件排序没有特别要求。如果读者发现相关有序命名法欢迎留言。
文件批量重命名
import java.io.File;
public class RenameFileTest {
private static String[] getFileName(String path){
File f = new File(path);
return f.list();
}
private static void renameFile(String path, String oldName, String newName){
if(!oldName.equals(newName)){
int dotAt = oldName.indexOf('.');
String _oldNamePre = oldName.substring(0,dotAt);
String _oldNameLast = oldName.substring(dotAt);
File oldFile = new File(path+_oldNamePre+_oldNameLast);
File newFile = new File(path+newName+_oldNameLast);
if(!oldFile.exists()){
System.out.println("File is not exists.");
return;
}
if(newFile.exists())
System.out.println(newName+" already exists.");
else{
oldFile.renameTo(newFile);
}
}else{
System.out.println("New file name is the same as old one");
}
}
public static void main(String[] arg){
String path = "C:\\Users\\CongTsang\\Pictures\\壁纸\\";
String fileName[] = getFileName(path);
System.out.println("There are " + fileName.length + " files.");
for (int i = 0; i < fileName.length; i++){
renameFile(path,fileName[i],""+(i+1));
}
}
}
- 修改文件前:
- 修改文件后:
所以你应该知道什么叫无序命名了吧。留下了没技术的眼泪。
叨叨几句... 4 条评论
可以,这个很详细
@太傅
感谢大佬夸奖
搞一个批量重命名文件的小程序阿。
@太傅
待我研究研究