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. boost_1_63_0在Win10上VS2015编译

    装了个最新版的boost库,各种尝试,各种看网上的文章,然而就是没有编译成功.我真是哭晕在厕所. 最后还是自己老老实实啃官方文档.终于编出来了.下面记录下方法. 一·最简单的一种方法. 1.直接打开命 ...

  2. Swift - what's the difference between metatype .Type and .self?

    Declaration typealias AnyClass = AnyObject.Type .Type The metatype of a class, structure, or enumera ...

  3. 图表库 - Highchart / Echart

    当前主要使用HighChart和Echart图表库,都基于Jquery,需要先引用Jquery. 实际问题:引入Jquery需在图表库前,否则报错. HighChart官网:https://www.h ...

  4. Java中Scanner类的使用

    一个可以解析基本类型和字符串的简单文本扫描器. 例如,以下代码使用户能够从 System.in 中读取一个数: public class ApiScanner { public static void ...

  5. UWP添加数字证书导出安装包本地安装

    先生成一个简单的HelloWorld应用程序 <Page x:Class="HelloWorld.MainPage" xmlns="http://schemas.m ...

  6. java 常用API 包装

    package com.oracel.demo01; public class Baozhuang { //将字符串转成基本数据类型 public static void main(String[] ...

  7. python中的网页标签等字符处理

    # -*- coding: utf-8-*- import re ##过滤HTML中的标签 #将HTML中标签等信息去掉 #@param htmlstr HTML字符串. def filter_tag ...

  8. 【转载】springboot注解

    https://blog.csdn.net/yitian_66/article/details/80866571 springboot注解:@Service: 注解在类上,表示这是一个业务层bean@ ...

  9. js中浏览器兼容startsWith 、endsWith 函数

    在做js开发的时候用到了startsWith函数时,发现各个浏览器不兼容问题,因为对开发来说,chrome浏览器最好用,就一直在chrome浏览器中使用这两个函数没有任何问题,但在ie浏览器访问就直接 ...

  10. buntu Rhythmbox解决中文乱码

    Ubuntu Rhythmbox解决中文乱码 在这里介绍的是一个解决方法,修改变量. 在终端输入: gedit ~/.profile 在最后加入下面内容: exportGST_ID3_TAG_ENCO ...