FILE类常量
1import java.io.*; 2class hello{ 3 public static void main(String[] args) { 4 System.out.println(File.separator);//输出"/" 5 System.out.println(File.separatorChar + "");//输出"/" 6 System.out.println(File.pathSeparator);//输出 ":" 7 System.out.println(File.pathSeparatorChar + "");//输出 ":" 8 } 9}
在windows下使用\进行分割,但是在linux下就不是\了。所以,要想使得我们的代码跨平台,更加健壮,所以,采用这两个常量吧
创建文件
1import java.io.*; 2class hello{ 3 public static void main(String[] args) { 4 File f=new File("D:\\hello.txt"); 5 try{ 6 f.createNewFile(); 7 }catch (Exception e) { 8 e.printStackTrace(); 9 } 10 } 11}
创建一个文件夹
1import java.io.*; 2class hello{ 3 public static void main(String[] args) { 4 String fileName="D:"+File.separator+"hello"; 5 File f=new File(fileName); 6 f.mkdir(); 7 } 8}
删除一个文件
1import java.io.*; 2class hello{ 3 public static void main(String[] args) { 4 String fileName="D:"+File.separator+"hello.txt"; 5 File f=new File(fileName); 6 if(f.exists()){ 7 f.delete(); 8 }else{ 9 System.out.println("文件不存在"); 10 } 11 } 12}
列出指定目录的全部文件名(包括隐藏文件)
1/** 2 * 使用list列出指定目录的全部文件名 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) { 7 String fileName="D:"+File.separator; 8 File f=new File(fileName); 9 String[] str=f.list(); 10 for (int i = 0; i < str.length; i++) { 11 System.out.println(str[i]); 12 } 13 } 14}
列出指定目录的全部文件(包括隐藏文件)
1/** 2 * 使用listFiles列出指定目录的全部文件 3 * listFiles输出的是完整路径 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) { 8 String fileName="D:"+File.separator; 9 File f=new File(fileName); 10 File[] str=f.listFiles(); 11 for (int i = 0; i < str.length; i++) { 12 System.out.println(str[i]); 13 } 14 } 15}
判断一个指定的路径是否为目录
1/** 2 * 使用isDirectory判断一个指定的路径是否为目录 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) { 7 String fileName="D:"+File.separator; 8 File f=new File(fileName); 9 if(f.isDirectory()){ 10 System.out.println("YES"); 11 }else{ 12 System.out.println("NO"); 13 } 14 } 15}
搜索指定目录的全部内容
1/** 2 * 列出指定目录的全部内容 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) { 7 String fileName="D:"+File.separator; 8 File f=new File(fileName); 9 print(f); 10 } 11 public static void print(File f){ 12 if(f!=null){ 13 if(f.isDirectory()){ 14 File[] fileArray=f.listFiles(); 15 if(fileArray!=null){ 16 for (int i = 0; i < fileArray.length; i++) { 17 //递归调用 18 print(fileArray[i]); 19 } 20 } 21 } 22 else{ 23 System.out.println(f); 24 } 25 } 26 } 27}
使用RandomAccessFile写入文件
1/** 2 * 使用RandomAccessFile写入文件 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) throws IOException { 7 String fileName="D:"+File.separator+"hello.txt"; 8 File f=new File(fileName); 9 RandomAccessFile demo=new RandomAccessFile(f,"rw"); 10 demo.writeBytes("asdsad"); 11 demo.writeInt(12); 12 demo.writeBoolean(true); 13 demo.writeChar('A'); 14 demo.writeFloat(1.21f); 15 demo.writeDouble(12.123); 16 demo.close(); 17 } 18}
中文乱码,不能对文件进行覆写不能追加
向文件中写入字符串
1/** 2 * 字节流 3 * 向文件中写入字符串 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 OutputStream out =new FileOutputStream(f); 11 String str="你好"; 12 byte[] b=str.getBytes(); 13 out.write(b); 14 out.close(); 15 } 16}
中文不乱码,文件不存在会创建,会覆写
一个字节一个字节的写
1/** 2 * 字节流 3 * 向文件中一个字节一个字节的写入字符串 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 OutputStream out =new FileOutputStream(f); 11 String str="你好"; 12 byte[] b=str.getBytes(); 13 for (int i = 0; i < b.length; i++) { 14 out.write(b[i]); 15 } 16 out.close(); 17 } 18}
向文件中追加新内容
1/** 2 * 字节流 3 * 向文件中追加新内容: 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 OutputStream out =new FileOutputStream(f,true);//追加模式 11 String str="Rollen"; 12 //String str="\r\nRollen"; 可以换行 13 byte[] b=str.getBytes(); 14 for (int i = 0; i < b.length; i++) { 15 out.write(b[i]); 16 } 17 out.close(); 18 } 19}
读取文件内容
1/** 2 * 字节流 3 * 读文件内容 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 InputStream in=new FileInputStream(f); 11 byte[] b=new byte[1024]; 12 in.read(b); 13 in.close(); 14 System.out.println(new String(b)); 15 } 16}
创建合适长度的空间大小
1/** 2 * 字节流 3 * 读文件内容,节省空间 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 InputStream in=new FileInputStream(f); 11 byte[] b=new byte[(int)f.length()]; 12 in.read(b); 13 System.out.println("文件长度为:"+f.length()); 14 in.close(); 15 System.out.println(new String(b)); 16 } 17}
一个一个读
1/** 2 * 字节流 3 * 读文件内容,节省空间 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 InputStream in=new FileInputStream(f); 11 byte[] b=new byte[(int)f.length()]; 12 for (int i = 0; i < b.length; i++) { 13 b[i]=(byte)in.read(); 14 } 15 in.close(); 16 System.out.println(new String(b)); 17 } 18}
文件不知道长度读取
1/** 2 * 字节流 3 *读文件 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 InputStream in=new FileInputStream(f); 11 byte[] b=new byte[1024]; 12 int count =0; 13 int temp=0; 14 while((temp=in.read())!=(-1)){//返回-1则表示到了文件尾部 15 b[count++]=(byte)temp; 16 } 17 in.close(); 18 System.out.println(new String(b)); 19 } 20}
字符流写入数据
1/** 2 * 字符流 3 * 写入数据 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 Writer out =new FileWriter(f);//追加时和上面一样加一为true的参数 11 String str="hello"; 12 out.write(str); 13 out.close(); 14 } 15}
字符流从文件中读出内容
1/** 2 * 字符流 3 * 从文件中读出内容 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 char[] ch=new char[100]; 11 Reader read=new FileReader(f); 12 int count=read.read(ch); 13 read.close(); 14 System.out.println("读入的长度为:"+count); 15 System.out.println("内容为"+new String(ch,0,count)); 16 } 17}
循环读取的方式
1/** 2 * 字符流 3 * 从文件中读出内容 4 * */ 5import java.io.*; 6class hello{ 7 public static void main(String[] args) throws IOException { 8 String fileName="D:"+File.separator+"hello.txt"; 9 File f=new File(fileName); 10 char[] ch=new char[100]; 11 Reader read=new FileReader(f); 12 int temp=0; 13 int count=0; 14 while((temp=read.read())!=(-1)){ 15 ch[count++]=(char)temp; 16 } 17 read.close(); 18 System.out.println("内容为"+new String(ch,0,count)); 19 } 20}
OutputStreramWriter将输出的字符流转化为字节流
InputStreamReader将输入的字节流转换为字符流
将字节输出流转化为字符输出流
1/** 2 * 将字节输出流转化为字符输出流 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) throws IOException { 7 String fileName= "d:"+File.separator+"hello.txt"; 8 File file=new File(fileName); 9 Writer out=new OutputStreamWriter(new FileOutputStream(file)); 10 out.write("hello"); 11 out.close(); 12 } 13}
将字节输入流变为字符输入流
1/** 2 * 将字节输入流变为字符输入流 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) throws IOException { 7 String fileName= "d:"+File.separator+"hello.txt"; 8 File file=new File(fileName); 9 Reader read=new InputStreamReader(new FileInputStream(file)); 10 char[] b=new char[100]; 11 int len=read.read(b); 12 System.out.println(new String(b,0,len)); 13 read.close(); 14 } 15}
ByteArrayInputStream 主要将内容写入内容
ByteArrayOutputStream 主要将内容从内存输出
使用内存操作流将一个大写字母转化为小写字母
1/** 2 * 使用内存操作流将一个大写字母转化为小写字母 3 * */ 4import java.io.*; 5class hello{ 6 public static void main(String[] args) throws IOException { 7 String str="ROLLENHOLT"; 8 ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes()); 9 ByteArrayOutputStream output=new ByteArrayOutputStream(); 10 int temp=0; 11 while((temp=input.read())!=-1){ 12 char ch=(char)temp; 13 output.write(Character.toLowerCase(ch)); 14 } 15 String outStr=output.toString(); 16 input.close(); 17 output.close(); 18 System.out.println(outStr); 19 } 20}
内容操作流一般使用来生成一些临时信息采用的,这样可以避免删除的麻烦。
版权声明:本文为博主原创文章,未经博主允许不得转载。