Java中对文件的操作是以流的方式进行的。流是指一连串流动的字符,是以先进先出方式发送信息的通道。
文件的基本属性在Java中对应的方法:
File f = new File("aaa.txt");
System.out.println("文件大小:"+f.length());
System.out.println("文件名:"+f.getName());
System.out.println("是文件:"+f.isFile());
System.out.println("是文件夹:"+f.isDirectory());
System.out.println("是可读:"+f.canRead());
System.out.println("是可写:"+f.canWrite());
System.out.println("是隐藏:"+f.isHidden());
System.out.println("文件是否存在:"+f.exists());
System.out.println("文件的绝对路径:"+f.getAbsolutePath());
System.out.println("文件的相对路径:"+f.getPath());
************************************************************************************************************
public class ShowDir {
public static void main(String[] args) {
//遍历D盘的所有文件和文件夹
File f = new File("d:");
show(f,"");
}
private static void show(File f ,String path) {
File[] fs = f.listFiles();
for (File file : fs) {
System.out.println(path + file.getName());
if(file.isDirectory()){
show(file, path+"-");
}
}
}
}
流按照方向分输入流(读)和输出流(写)
************************************************************************************************************
输入流(读):input——read
public class TestInput {
public static void main(String[] args) throws IOException {
//通过输入流读取文件
try {
FileInputStream fis = new FileInputStream("d:/javach11/aaa.txt");
byte[] bs = new byte[fis.available()];
fis.read(bs);
for (int i = 0; i < bs.length; i++) {
System.out.print((char)bs[i]);
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
************************************************************************************************************
public class TestOutput {
public static void main(String[] args) throws IOException {
//通过输出流在文件中写内容
FileOutputStream fos = new FileOutputStream("D:/workspace/javach11/aaa.txt",true);
String str = "aaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] bs = str.getBytes();
fos.write(bs);
fos.close();
}
}
************************************************************************************************************
流按照内容分字节流(byte)和字符流(char)
************************************************************************************************************
public class TestChar {
public static void main(String[] args) throws IOException {
//涉及到中文字符的读写时用字符流
FileWriter fw = new FileWriter("D:/aaa.txt",true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write("你好");
bw.newLine();
bw.write("出去吗");
bw.write("不出去");
bw.close();
fw.close();
FileReader fr = new FileReader("D:/aaa.txt");
BufferedReader br= new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
fr.close();
}
}
************************************************************************************************************
public class TestSysOut {
public static void main(String[] args) throws IOException{
//通过控制台书写内容到某个文件
FileOutputStream fos = new FileOutputStream("D:/workspace/javach11/aaa.txt");
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.println("这是由java,System.out.println()输出的内容");
System.out.println("看看换行好使不");
ps.close();
fos.close();
}
}
************************************************************************************************************
一般情况下,通过类名来判断流的类型。类名中Stream的都是字节流,有Writer或Reader的是字符流。
Input、Reader都是输入流,
Output、Writer是输出流。