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