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的一些列子的更多相关文章

  1. Java IO流 BufferedInputStream、BufferedOutputStream的基本使用

    BufferedInputStream.BufferedOutputStream的基本使用 BufferedInputStream是FilterInputStream流的子类,FilterInputS ...

  2. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  3. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  4. Java IO之字符流和文件

    前面的博文介绍了字节流,那字符流又是什么流?从字面意思上看,字节流是面向字节的流,字符流是针对unicode编码的字符流,字符的单位一般比字节大,字节可以处理任何数据类型,通常在处理文本文件内容时,字 ...

  5. java Io流向指定文件输入内容

    package com.hp.io; import java.io.*; public class BufferedWriterTest{ public static void main(String ...

  6. java Io文件输入输出流 复制文件

    package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...

  7. java Io流更新文件内容

    package com.hp.io; import java.io.FileOutputStream; import java.io.IOException; public class FileOut ...

  8. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  9. java.io.NotSerializableException: test.io.file.Student

    java.io.NotSerializableException: test.io.file.Student    at java.io.ObjectOutputStream.writeObject0 ...

随机推荐

  1. Oracle创建database link(dblink)和同义词(synonym)

    同一个数据库不同用户之间建立dblink和synonym 1.建立dblink 实现在A用户下通过dblink访问B用户下的数据库表,需要在A用户下创建访问B库的dblink连接 --创建远程连接db ...

  2. SpringMVC学习三

    实现有点用处的增删改查,并利用了AJAX(javascript)动态修改,还有json的返回读取,以及文件上传和下载. 配置基础Employee类以及Dao类 package com.springmv ...

  3. php session (二): 同样的代码 ,不同域名访问 对session存储的影响.

    一:准备工作: 位置: htdocs a目录: sessiona.php b目录 sessionb.php c目录 sessionc.php ------ sessiona.php <?php ...

  4. Vue extend 学习

    <div id="box"> <aa></aaa> </div> <script> var Aaa = Vue.exte ...

  5. 2-log4j2之使用根控制器输出日志到控制台

    一.添加maven依赖 <!-- 使用aliyun镜像 --> <repositories> <repository> <id>aliyun</i ...

  6. ChainingHash

    public class ChainingHash<Key,Value>{ private int N; private int M; private doublylinked<Ke ...

  7. stack && queue

    package elementary_data_structure; import java.util.Iterator;import java.util.NoSuchElementException ...

  8. day12作业答案

    2.1 # lst=['asdgg','as','drtysr'] # lst2=[i.upper() for i in lst if len(i) >3 ] # print(lst2) # 2 ...

  9. 【转载】 Pytorch中的学习率调整lr_scheduler,ReduceLROnPlateau

    原文地址: https://blog.csdn.net/happyday_d/article/details/85267561 ------------------------------------ ...

  10. MATLAB的一些小经验,记下来,facilitate future work

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/03/28 0.杂.这个帖子(https://www.zhihu.com/question/24499729) ...