Java IO的一些列子
Write()方法写入文件
public static void main(String[] args){
try{
BufferedWriter out = new BufferedWriter(new FileWriter("D:\\temp\\runoon.txt"));//创建文件
out.write("cainiaojiaocheng");//向文件中写入字符串
out.close();//关闭文件。运行之后查看目录下应该生成文件,并且文件中有写入的字符串
System.out.println("create file successfully");
}catch(IOException e){
}
}
//用readLine()从文件中读取数据
try{
BufferedReader in = new BufferedReader(new FileReader("D:\\temp\\runoon.txt"));//获取文件地址
String str;//创建一个字符串对象来缓存下读出来的字符串
while((str=in.readLine())!=null){
System.out.print(str);
}
}catch(IOException e){
System.out.println(e);
}
创建临时文件,向文件中对家数据
import java.io.*; /**
* Created by Sandy.Liu on 2017/8/8.
*/
public class CreateTempFile {
public static void main(String[] args){
try {
File fileDir = new File("D://Temp//");
File tempFile = File.createTempFile("createTempFile", ".txt",fileDir);
tempFile.deleteOnExit();
BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
bw.write("String 1");
bw.close();
System.out.println("temp file is created"); //向文件中追加数据,这里注意不要创建一个新的BufferedWriter对象,要用之前的对象,不然会再创建一个新的tempfile,而不是之前的那个对象追加
bw = new BufferedWriter(new FileWriter(tempFile,true));
bw.write("string 2");
bw.close();
BufferedReader br = new BufferedReader(new FileReader(tempFile));
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
}catch (IOException e){
System.out.println(e);
}
}
}
重命名
import java.io.*; /**
* Created by Sandy.Liu on 2017/8/8.
*/
public class FileRename {
public static void main(String[] args){
File oldName = new File("D://Temp//TestSource.txt");
File newName = new File("D://Temp//TestSource1.txt");
if(oldName.renameTo(newName)){
System.out.println("rename successfully");
}else{
System.out.println("Error");
}
}
} 复制文件,用到BufferedWriter,BufferedReader,InputStream,OutputStream
import java.io.*; /**
* Created by Sandy.Liu on 2017/8/8.
*/
public class CopyFile {
public static void main(String[] args){
try {
//创建文件,并向文件中写入数据
BufferedWriter bf = new BufferedWriter(new FileWriter("D://Temp//TestSource.txt"));
bf.write("this is for test copy files from TestSource.txt to TestTarget.txt");
bf.close(); //读出文件到buffer里,为写入另一个文件做准备
InputStream is = new FileInputStream(new File("D://Temp//TestSource.txt"));
byte[] buf = new byte[1024]; //把buffer里的数据写入到目标文件中去
OutputStream ot = new FileOutputStream(new File("D://Temp//TestTarget.txt"));
int len;
while((len=is.read(buf))>0){
ot.write(buf,0,len);
} //从目标文件中读取字符串,输出到控制台
BufferedReader br = new BufferedReader(new FileReader("D://Temp//TestTarget.txt"));
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
}catch(IOException e){
System.out.println(e);
}
}
}
Java IO的一些列子的更多相关文章
- Java IO流 BufferedInputStream、BufferedOutputStream的基本使用
BufferedInputStream.BufferedOutputStream的基本使用 BufferedInputStream是FilterInputStream流的子类,FilterInputS ...
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- Java IO之字符流和文件
前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...
- java Io流向指定文件输入内容
package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...
- java Io文件输入输出流 复制文件
package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- java Io流更新文件内容
package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- java.io.NotSerializableException: test.io.file.Student
java.io.NotSerializableException: test.io.file.Student at java.io.ObjectOutputStream.writeObject0 ...
随机推荐
- SQL3-查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no
题目描述 查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_noCREATE TABLE `dept_manager` (`dept_no` ch ...
- TBody scrollbar 设置
由于scrollbar自身有宽度 对于tbody来说可能会挤压与thead不对齐下面办法能够解决大致问题 1.设置tbody display:block : overflow-y:auto:(并且修 ...
- 监控中的TP50
TP指标: TP50:指在一个时间段内(如5分钟),统计该方法每次调用所消耗的时间,并将这些时间按从小到大的顺序进行排序,取第50%的那个值作为TP50 值:配置此监控指标对应的报警阀值后,需要保证在 ...
- wxPython的使用--类似画板的界面
# -*- coding: utf-8 -*-import wximport wx.lib.buttonsimport cPickleimport os class PaintWindow(wx.Wi ...
- 解决java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor问题
hibernate整合spring当在spring配置文件中加入如下代码 <!--2.配置事务属性,需要事务管理器--> <tx:advice id="txAdvice&q ...
- 利用itext生成pdf的简单例子
一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...
- makefile 使用 Tricks
.phony是表示目标是伪目标,并不生成相应的文件..phony标志的文件总是执行的. 1. 短横(-)与@ @(常用在 echo 之前):make 在执行编译打包等命令前会在命令行输出此命令,称之为 ...
- Samsung_tiny4412(驱动笔记06)----list_head,proc file system,GPIO,ioremap
/**************************************************************************** * * list_head,proc fil ...
- XXS level2
(1)用level1的方法尝试,发现行不通 (2)查看PHP源代码 <?php ini_set("display_errors", 0); $str = $_GET[&quo ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...