package tlistpackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; /**
* @author Administrator
*
*/
public class listdemo {
public static void main(String[] args) throws IOException
{
// System.out.println(Runtime.getRuntime().getClass());
// System.out.println(Math.round(23.55));
// System.out.println(Math.ceil(26.333));
// System.out.println((int)(Math.random()*10+1));
// Date d=new Date();
// System.out.println(d.toString());
// //格式化时间
// System.out.println(DateFormat.getDateInstance().format(d));
// char[] buffer=new char[1024];
// int len=0;
// while((len=rf.read(buffer))!=-1)
// {
// System.out.print(new String(buffer,0,len));
// }
// WriteFile("txtdemo.txt","dirk.wang");
// String textString= ReadFile("txtdemo.txt");
// System.out.print(textString);
// InputWriteFile("txtdemo.txt","dirk.wang,show time");
// String str= InputReadFile("txtdemo.txt");
// System.out.print(str);
CopyFile("txtdemo.txt","new.txt");
} /**
* @param path 路径
* @return
*/
@SuppressWarnings("finally")
public static String ReadFile(String path)
{
FileReader fs=null;
String str="";
try {
fs=new FileReader(path);
char[] buffer =new char[1024];
int len=0; while((len=fs.read(buffer))!=-1)
{
str+=new String(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
finally {
if(fs!=null)
{
try {
fs.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return str;
}
} /**
* @param path 路径
* @param text 写入的内容
*/
public static void WriteFile(String path,String text)
{
FileWriter fw =null;
try {
//true 表示追加写入文件
fw=new FileWriter(path,true);
fw.write(text);
//fw.write(text);
fw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if(fw!=null)
{
try {
fw.close();
} catch (IOException e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
} /**
* 通过二进制流读取文件
* @param path 路径
* @return
*/
public static String InputReadFile(String path)
{
File file=new File(path);
StringBuilder str=new StringBuilder();
if(file.isFile())
{
FileInputStream fs=null;
try {
fs=new FileInputStream(file);
byte[] buffer=new byte[1024];
int len=0;
while((len=fs.read(buffer))!=-1)
{
str.append(new String(buffer,0,len));
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if(fs!=null)
{
try {
fs.close();
} catch (IOException e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
} return str.toString();
} /**
* @param path 路径
* @param text 内容
*/
public static void InputWriteFile(String path,String text)
{
File file=new File(path);
FileOutputStream fos=null;
if(file.isFile())
{
try {
fos=new FileOutputStream(file,true);
fos.write(text.getBytes());
fos.flush();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if(fos!=null)
{
try {
fos.close();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
} }
} public static void CopyFile(String oldPath,String newPath)
{
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(oldPath);
fos=new FileOutputStream(newPath);
int len=0;
byte[] buffer =new byte[1024];
while((len=fis.read(buffer))!=-1)
{
fos.write(buffer,0,len);
fos.flush();
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
fis.close();
fos.close();
} catch (IOException e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}

  

Java io 操作的更多相关文章

  1. JAVA—IO操作

    一.JAVA I/O 输入输出流 1:编码问题 2:File类的使用 3:RandomAccessFile的使用 4:字节流的使用 5:字符流的使用 6:对象的序列化和反序列化 2: file类的使用 ...

  2. java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例

    FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...

  3. Java IO操作——数据操作流DataOutputStream和DataInputStream的使用

    学习目标  掌握DataOutputStream和DataInputStream的作用 可以使用DataOutputStream和DataInputStream写入和读入数据 数据操作流 在io包中, ...

  4. Java IO操作:合并流

    合并流:把两个文件合并在一起. 主要操作的是内容. 定义: public class SequenceInputStreamextends InputStream 方法摘要: 操作步骤: 1,分别建立 ...

  5. JAVA IO操作:数据操作流:DataOutputStream和DataInputStream

    掌握DataOutputStream和DataInputStream的作用. 可以使用DataOutputStream和DataInputStream写入和读取数据. 在IO包中提供了两个与平台无关的 ...

  6. java IO操作,看完你应该就清晰了。

    前言: java中IO里的一些知识对于一个java新手来说,是比较难理解的.因为里面存在一些很绕的概念,比如: 1.到底是读入写出,还是读出写入: 2.我要将一个文件的内容拷贝到另一个文件是先用Inp ...

  7. java IO操作和计算操作:工作内存和主内存 volatile关键字作用;原子操作对象AtomicInteger ....

    应该停止但无法停止的计算线程 如下线程示例,线程实例中while循环中的条件,在主线程中通过调用实例方法更新后,while循环并没有更新判断变量是否还成立.而是陷入了while(true)死循环. i ...

  8. Java IO操作

    转自:http://www.cnblogs.com/jyan/articles/2505791.html Johnny Yan的博客 1 InputStream类型 InputStream的作用是标志 ...

  9. 每天进步一点点-Java IO操作-Java Serializable(对象序列化)的理解和总结

    往硬盘文件里写数据 序列化:序列化是将对象转换为容易传输的格式的过程.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.在另一端,反序列化将从该流重 ...

随机推荐

  1. JavaScript创建对象的几种 方式

    //JavaScript创建对象的七种方式 //https://xxxgitone.github.io/2017/06/10/JavaScript%E5%88%9B%E5%BB%BA%E5%AF%B9 ...

  2. 接口测试及Postman工具

    首先,什么是接口呢? 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口.系统对外的接口:比如你要从别的网站或服务器上获取资源或信息,别人肯定不会把数据库共享给你,他只能给你提供一个他们写 ...

  3. Centos7 单节点安装 FastDFS + FastDHT服务

    Centos7 单节点安装 FastDFS + FastDHT服务 1.安装gcc(编译时需要) FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果 ...

  4. javase 继承练习

    package xuexi; 父级 public class Fu { int num=0; public void eat() { System.out.println("父亲在吃饭&qu ...

  5. JS 从36个数字里面随机抽取8个

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. shell分析nginx日志

    以下脚本都是基于上面日志格式的,如果你的日志格式不同需要调整awk后面的参数. 分析日志中的UserAgent cat access_20130704.log | awk -F "\&quo ...

  7. 路飞学城Python-Day108

    96-分页器1 批量插入的方式就不能用ORM的create()方式去做了,因为create就是对sql进行insert的操作,sql最好不要每次有一条数据就去进行插入,最好的方式就是插入一组数据 fr ...

  8. linux--ubuntu的下载以及VMware Tool的安装

    1. Ubuntu的下载:http://cn.ubuntu.com/download/ 2. VMware Tool的安装: 第一步:在主机上,从 Workstation Pro 菜单栏中选择虚拟机 ...

  9. python爬网页中文乱码问题

    再用python爬取网页时,用模拟浏览器登陆,得到的中文字符出现乱码,该怎么解决呢? url = “http://newhouse.hfhouse.com/” req = urllib2.Reques ...

  10. ClipboardJS实现点击复制功能

    <script src="//lib.baomitu.com/clipboard.js/1.7.1/clipboard.min.js"></script> ...