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. centos7创建docker tomcat镜像

    1 准备宿主系统 准备一个 CentOS 7操作系统,具体要求如下: 必须是 64 位操作系统 建议内核在 3.8 以上 通过以下命令查看您的 CentOS 内核: 1 # uname -r 2 安装 ...

  2. Java基础知识补充

    基础知识总结: 学习了一段时间,重新看了孤傲苍狼的博客,对一些知识有了新的理解. unicode: 全球的文字放到计算机里面表示全是0和1,Unicode是统一了全世界国家文字的一种编码方式,用这样的 ...

  3. Java与C++简单对比

    Java语言让编程者无法找到指针来直接访问内存,并且增添了自动的内存管理功能,从而有效的组织了C/C++语言中指针操作失误,如滥用指针所造成的系统崩溃,Java的指针在虚拟机内部使用,这保证了Java ...

  4. pragma comment的使用 pragma预处理指令详解

    pragma comment的使用 pragma预处理指令详解   #pragma comment( comment-type [,"commentstring"] ) 该宏放置一 ...

  5. Android 删除图片等资源文件 通知系统更新,重新扫描

    public void delPic(String path){ File delFile = new File(path); if (delFile.exists()) { delFile.dele ...

  6. 百度地图API示例:鼠标绘制点线面 控件修改

    需求 :在使用地图API时,绘制工具栏控件想自己选择哪些要,哪些不要. 可以查看相应的类:官网地址: http://api.map.baidu.com/library/DrawingManager/1 ...

  7. <Spark><Tuning and Debugging>

    Overview 这一部分我们主要讨论如果配置一个Spark application,如何tune and debug Spark workloads 配置对Spark应用性能调优很重要.我们有必要理 ...

  8. day 68 增删改查 语法

    1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog)     blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...

  9. scrapy面试一

    1.动态加载又对及时性要求很高怎么处理? Selenium+Phantomjs 尽量不使用 sleep 而使用 WebDriverWait 2.分布式爬虫主要解决什么问题? (1)ip (2)带宽 ( ...

  10. 50_流程控制函数-case结构

    case函数的使用一:switch case 的效果 /* Java中 switch(变量或表达式){ case 常量1:语句1:break: ... default:语句n;break; } MyS ...