Java 文件io流
程序员文章站
2024-01-21 18:39:58
...
package fileInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.CharBuffer;
public class FileInputStreamTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// testFileMethod("D:\\workspace\\datafile","jjn.txt");
// lsFile("D:\\workspace\\1");
File file = new File("D:\\workspace\\datafile","jjn.txt");
String str = "我是一只小小小鸟,飞呀飞呀,飞不高!!";
writeFile(file, str);
readFile(file);
testFileMethod("D:\\workspace\\datafile","jjn.txt");
}
/**
* 创建File文件
* @param parent 父路径字符串
* @param fileName 子路径字符串,文件名
*/
public static void createFile(String parent,String fileName) {
File f = new File(parent,fileName);
if(!f.exists()) {
try {
// 创建文件
f.createNewFile();
System.out.println("文件被创建");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 删除文件
* @param parent 父路径字符串
* @param fileName 子路径字符串,文件名
*/
public static void deleteFile(String parent,String fileName){
File f = new File(parent,fileName);
if(f.exists()) {
f.delete();
System.out.println("文件被删除");
}
}
public static void testFileMethod(String parent,String fileName) {
File file = new File(parent,fileName);
// test exists()方法
boolean file_exists = file.exists();
System.out.println("file exists:"+file_exists);
if(!file_exists) {
try {
file.createNewFile();
System.out.println("文件被创建");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
// test getName()方法
String fileNmae = file.getName();
System.out.println("fileName:"+fileName);
// test canRead()方法
boolean flage = file.canRead();
System.out.println(fileName+"can read :"+flage);
// test canWrite()方法
flage = file.canWrite();
System.out.println(fileName+"can write :"+flage);
// test length()方法 返回值为字节长度
long length = file.length();
System.out.println("file length :"+length);
// test getAbsolutePath()方法
String path = file.getAbsolutePath();
System.out.println("absolute path:"+path);
// test getParent()方法
String parentPath = file.getParent();
System.out.println("parent path:"+parentPath);
flage = file.isFile();
System.out.println(fileNmae+" is file :"+flage);
flage = file.isDirectory();
System.out.println(fileNmae+" is directory :"+flage);
flage = file.isHidden();
System.out.println(fileNmae+" is hidden :"+flage);
long modifieTime = file.lastModified();
System.out.println(fileNmae+" last modified time :"+modifieTime);
}
}
/**
* 边路path目录下的所有文件
* @param path 文件路径
*/
public static void lsFile(String path) {
File file = new File(path);
if(file.exists()) {
if(file.isFile())
{
//是文件
System.out.println(file.getAbsolutePath());
return;
}
if(file.isDirectory()) {
String[] files=file.list();
for(int i=0;i<files.length;i++)
{
lsFile(file.getAbsolutePath()+File.separatorChar+files[i]);
}
}
}else {
System.out.println("路径不存在");
}
}
/**
* 写文件
* @param file File对象
* @param bt byte[] 要写入的数据
*/
public static void writeFile(File file,String str) {
try {
// 在文件的文末增加新内容
FileWriter fw = new FileWriter(file,true);
BufferedWriter buwt = new BufferedWriter(fw);
buwt.write(str);
buwt.write("\n");
buwt.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*读文件
* @param file File对象
*/
public static void readFile(File file) {
try {
FileReader fr = new FileReader(file);
BufferedReader bufr = new BufferedReader(fr);
String str= null;
int i = 0;
while(null != (str = bufr.readLine())) {
i++;
System.out.println("第"+i+"行:"+str);
}
System.out.println("文件大小:"+file.length()+"字节");
bufr.close();
fr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上一篇: java设计模式六(抽象工厂模式)
下一篇: java IO流,文件