IO整理
来源:互联网 发布:linux vim 复制粘贴 编辑:程序博客网 时间:2024/04/29 12:35
//创建按一个文件public static void main(String[] args) {File f = new File("j:\\hello.txt");try {f.createNewFile();} catch (Exception e) {e.printStackTrace();}}
程序运行之后,在d盘下会有一个名字为hello.txt的文件。
IO整理链接 http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html
/*输出的是"/",和";"此处多说几句:有些同学可能认为,我直接在windows下使用\进行分割不行吗?当然是可以的。但是在linux下就不是\了。所以,要想使得我们的代码跨平台,更加健壮,所以,大家都采用这两个常量吧,其实也多写不了几行*/ public static void test(){ System.out.println(File.separator);System.out.println(File.pathSeparator); } //现在我们使用File类中的常量改写上面的代码: public static void test2(){ String fileName = "J:"+File.separator+"hello.txt"; File f = new File(fileName); try {f.createNewFile();} catch (Exception e) {e.printStackTrace();} }
//删除文件 public static void test(){ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); if(f.exists()){ f.delete(); }else{ System.out.println("没有此文件"); } }
//创建一个文件夹 public static void test(){ String fileName = "j:"+File.separator+"hello"; File f = new File(fileName); f.mkdir(); }
//列出指定目录的全部文件(包括隐藏文件):使用list列出指定目录的全部文件 public static void test(){ String fileName = "j:"+File.separator; File f = new File(fileName); String[] str = f.list(); for (String s : str) {System.out.println(s);} }
//列出指定目录的全部文件(包括隐藏文件):使用list列出指定目录的全部文件 public static void test(){ String fileName = "j:"+File.separator; File f = new File(fileName); File[] str = f.listFiles(); for (File s : str) {System.out.println(s);} }
字节流
//向文件中写入字符串 public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); OutputStream out = new FileOutputStream(f); String str = "asfds"; byte[] b = str.getBytes(); out.write(b); out.close(); }
//向文件中一个字节一个字节的写入字符串 public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); OutputStream out = new FileOutputStream(f); String str = "asdfg"; byte[] b = str.getBytes(); for (byte c : b) {out.write(c);} out.close(); }
//向文件中追加新内容: public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); OutputStream out = new FileOutputStream(f,true); String str = "gfdsa"; byte[] b = str.getBytes(); for (byte c : b) {out.write(c);} out.close(); }
//读文件内容 public static void main(String[] args) throws IOException{ String fileName = "j:"+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)); }
//但是这个例子读取出来会有大量的空格,我们可以利用in.read(b);的返回值来设计程序。如下: public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); InputStream in = new FileInputStream(f); byte[] b = new byte[1024]; int len = in.read(b); in.close(); System.out.println("读入长度为:"+len); System.out.println(new String(b,0,len)); }
//读者观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,//有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以这样干://读文件内容,节省空间 public static void main(String[] args) throws IOException{ String fileName = "j:"+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)); }
//一个一个的读 public static void main(String[] args) throws IOException{ String fileName = "j:"+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 < f.length(); i++) {b[i] = (byte) in.read();} System.out.println("文件长度:" + f.length()); in.close(); System.out.println(new String(b)); }
//细心的读者可能会发现,上面的几个例子都是在知道文件的内容多大,然后才展开的,//有时候我们不知道文件有多大,这种情况下,我们需要判断是否独到文件的末尾。 public static void main(String[] args) throws IOException{ String fileName = "j:"+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){ b[count++] = (byte) temp; } in.close(); System.out.println(new String(b,0,count)); }
提醒一下,当独到文件末尾的时候会返回-1.正常情况下是不会返回-1的
字符流
//写入字符数据其实这个例子上之前的例子没什么区别,只是你可以直接输入字符串,而不需要你将字符串转化为字节数组。//当你如果想问文件中追加内容的时候,可以使用将上面的声明out的哪一行换为://Writer out =new FileWriter(f,true);//这样,当你运行程序的时候,会发现文件内容变为://hellohello如果想在文件中换行的话,需要使用“\r\n”//比如将str变为String str="\r\nhello";//这样文件追加的str的内容就会换行了。 public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); Writer out = new FileWriter(f); String b = "项目33"; out.write(b); out.close(); Writer out1 = new FileWriter(f,true); String b1 = "\r\nsafsdf"; out1.write(b1); out1.close(); }
//从文件中读出内容 public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); Reader read = new FileReader(f); char[] ch = new char[100]; int len = read.read(ch); read.close(); System.out.println("长度: "+len); System.out.println(new String(ch,0,len)); }
结果
长度: 12
项目33
safsdf
//从文件中读出内容 当然最好采用循环读取的方式,因为我们有时候不知道文件到底有多大。 public static void main(String[] args) throws IOException{ String fileName = "j:"+File.separator+"hello.txt"; File f = new File(fileName); Reader read = new FileReader(f); char[] ch = new char[100]; int count = 0; int temp = 0; while((temp=read.read())!=-1){ ch[count++] = (char) temp; } System.out.println("长度:"+count); System.out.println(new String(ch,0,count)); }
结果是
长度:12
项目33
safsdf
如果直接用new String(ch)会产生多余的空格
关于字节流和字符流的区别
实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。
读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,
但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。
使用字节流好还是字符流好呢?
答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
0 0
- IO整理
- IO整理
- IO流整理
- Java IO 流整理
- java中的IO整理
- java中的IO整理
- 文件IO小结整理
- java中的IO整理
- JAVA IO整理
- java中的IO整理
- java中的IO整理
- java中的IO整理
- java中的IO整理
- java中的IO整理
- java中的IO整理
- java中的IO整理
- socket.io入门整理
- java中的IO整理
- Java JDK7安装与环境变量配置详细图解
- 【暑期基础2】Y HDU 2050 折线分割平面
- java 对象序列化
- HTML5的画布:Paths
- OS开发UI篇—CAlayer(自定义layer)
- IO整理
- 关于iOS的GCD的介绍以及使用
- RMI多网卡问题
- mysql 命令行操作
- Android中如何控制LogCat的自定义输出
- Running Modetest to Test Your DRM Display Driver Once you finish the DRM(Direct Rendering Manager)
- java的反射
- Java泛型
- butterknife7.0的使用