字节流、字符流
- 能用字符流的一定能用字节流
- 能用字节流的不一定能用字符流
步骤
- 连接数据源 File类构建路径
- 拿到字节流或字符流对象
- 输入或者输出操作
- 关闭字节流或者关闭字符流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static void main(String[] args) throws IOException { String path = "C:\\Users\\SimpleManWp\\Desktop"; String filename = "123.txt"; File f=new File(path,filename); System.out.println("文件长度:"+f.length()+"字节"); 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("最后修改日期:"+new Date(f.lastModified())); System.out.println("文件名称:"+f.getName()); System.out.println("文件路径:"+f.getPath()); System.out.println("绝对路径:"+f.getAbsolutePath()); if(f.exists()) { f.delete(); }else { f.createNewFile(); }
}
|
注:File不是创建文件的函数,而是创建路径的函数;new File(),括号中填的参数列表只要能拼出路径就行
1 2 3 4 5 6 7 8
| String path = "C:\\Users\\SimpleManWp\\Desktop\\123";
File f = new File(path); if(f.exists()) { f.delete(); }else { f.mkdir(); }
|
1 2 3 4 5 6 7 8 9
| String path = "C:\\Users\\SimpleManWp\\Desktop"; File f = new File(path); String Filelist[] = f.list(); for (int i=0;i<Filelist.length;i++) { System.out.print(Filelist[i]+"\t\t"); System.out.print((new File("C:\\\\Users\\\\SimpleManWp\\\\Desktop",Filelist[i])).isFile()?"文件"+ "\t\t":"文件夹"+"\t\t"); System.out.println((new File("C:\\\\Users\\\\SimpleManWp\\\\Desktop",Filelist[i])).length()+"字节"); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class FileFilter implements FilenameFilter {
@Override public boolean accept(File dir, String name) { return name.endsWith(".txt")||name.endsWith(".exe"); }
} String path = "C:\\Users\\SimpleManWp\\Desktop"; File f = new File(path); String Filelist[] = f.list(new FileFilter()); for (int i=0;i<Filelist.length;i++) { System.out.print(Filelist[i]+"\t\t"); System.out.print((new File("C:\\\\Users\\\\SimpleManWp\\\\Desktop",Filelist[i])).isFile()?"文件"+ "\t\t":"文件夹"+"\t\t"); System.out.println((new File("C:\\\\Users\\\\SimpleManWp\\\\Desktop",Filelist[i])).length()+"字节"); }
|
参考
http://c.biancheng.net/view/1133.html
动态读取文件
RandomAccessFile篇
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package demo;
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException;
public class wjrw {
public static void main(String[] args) { String path = "C:\\Users\\SimpleManWp\\Desktop\\123.txt"; File file = new File(path); if(file.exists()) { file.delete(); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } }else { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } }
try { RandomAccessFile rf = new RandomAccessFile(file,"rw"); String str1="男儿横行当有术,区区莽夫难登天"; try { String str2=new String(str1.getBytes("GBK"),"iso8859-1"); try { rf.writeBytes(str2); System.out.println(rf.getFilePointer()); rf.seek(0); byte[] buffer=new byte[14]; rf.read(buffer); System.out.println(new String(buffer)); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } }
}
|
注:使用read()方法后,指针会进行移位
例: 文件中的句子为 “男儿横行当有术,区区莽夫难登天”,下面打印的就是”区区莽夫难登天”
1 2 3 4 5 6 7 8 9 10 11 12
| byte[] buffer1 = new byte[14]; byte[] buffer2 = new byte[2]; byte[] buffer3 = new byte[14];
rf.read(buffer1);
rf.read(buffer2);
rf.read(buffer3); System.out.print(new String(buffer3));
|
1 2 3 4 5
| byte[] buffer1 = new byte[2]; int len=0; while((len=rf.read(buffer1))!=-1) { System.out.print(new String(buffer1)); }
|
https://blog.csdn.net/weixin_41543601/article/details/88638342
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static void main(String[] args) throws IOException { String path = "C:/Users/SimpleManWp/Desktop"; String name = "测试文件.txt"; File file = new File(path,name);
InputStream input = new FileInputStream(file);
byte bytes[] = new byte[7]; input.read(bytes); input.close();
System.out.println(new String(bytes)); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static void main(String[] args) throws IOException { String path = "C:/Users/SimpleManWp/Desktop"; String name = "测试文件.txt"; File file = new File(path,name);
InputStream input = new FileInputStream(file);
byte bytes[] = new byte[(int)file.length()]; input.read(bytes); input.close();
System.out.println(new String(bytes));
|
- 第三种读取文件大小的数据,挨个读取字节,不存在下一个的时候停止
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static void main(String[] args) throws IOException { String path = "C:/Users/SimpleManWp/Desktop"; String name = "测试文件.txt"; File file = new File(path,name);
InputStream input = new FileInputStream(file);
byte bytes[] = new byte[(int)file.length()]; int len = 0 ; int temp = 0 ; while((temp=input.read())!=-1){ bytes[len] = (byte)temp ; len++ ; }
System.out.println(new String(bytes));
|
https://www.cnblogs.com/kongxianghao/articles/6879367.html
outputStream篇
FileOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public static void main(String[] args) throws IOException {
File intfile = new File("C:/Users/SimpleManWp/Desktop/桌面文件.txt");
FileInputStream input = new FileInputStream(intfile);
byte[] bytes = new byte[(int) intfile.length()];
input.read(bytes);
for(int i=0;i<bytes.length;i++){ System.out.println((char)bytes[i]); }
input.close();
String str = "66666" +"\r" +"\t"+"123"+new String(bytes);
File file = new File("C:/Users/SimpleManWp/Desktop/桌面文件.txt") ;
FileOutputStream output = new FileOutputStream(file);
output.write(str.getBytes());
output.close(); }
|