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. win10 安装 mysql-8.0.12

    安装mysql 8 1.下载 https://dev.mysql.com/downloads/mysql/ 2.设置环境变量 将你解压后的文件里边的bin目录加入到path中.例如:D:\develo ...

  2. jackson中的@JsonBackReference

    #    StackOverflowError / 无限递归 / json递归 / JsonBackReference 环境:springmvc+hibernate+json 在controller返 ...

  3. UBUNTU 测试跑分

    time echo "scale=5000; 4*a(1)" | bc -l -q3.14159265358979323846264338327950288419716939937 ...

  4. PMS5003ST+Arduino Nano OLED屏显示

    整合OLED显示和PMS5003报数 #include <Arduino.h> #include <pms.h> /////////////////////////////// ...

  5. golang对数组进行冒泡排序

    什么是冒泡排序? 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地 ...

  6. SQL语句报错,无法绑定由多个部分组成的标识符解决

    无法绑定由多个部分组成的标识符, 表示在查询的时候使用了别名,并且查询的多个表中存在相同的字段,如果在使用该字段时不明确该字段的来源就会报这个错误. 举例: 我们有两张表,B1,B2,他们有一个共同的 ...

  7. 2.3 xpath定位

    2.3 xpath定位 前言    在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详 ...

  8. math、numpy、pandas NaN 判断

    >> np.nan == np.nan False >> np.nan is np.nan True >> math.nan is np.nan False > ...

  9. [LeetCode&Python] Problem 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  10. lvm创建 及 扩空 等相关

    1.先对磁盘进行LVM 设置 2.pvcreate  物理卷的创建 pvcreate /dev/### 3.vgcreate 物理卷组的创建 vgcreate  vg_name  /dev/###  ...