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. 杭电2053 WA

    #include<stdio.h> int main() { ]; while(scanf("%d",&n)!=EOF) { ;i<=;i++) { a[ ...

  2. PhotoZoom Classic 7有什么用?高品质的放大模糊图片!

    PhotoZoom Classic 7专门用于放大照片,同时保持质量.该软件配备了BenVista独特的S-Spline技术,可轻松超越Photoshop的双三次插值等替代解决方案. PhotoZoo ...

  3. sublime 自定义快捷生成代码块

    菜单栏目选 Tools(工具) =>Developer(插件开发)=>New Snippet....(新建代码片段),如图: 接着会新开一个标签页,会附带一些内容:如图: 将“Hello, ...

  4. PHP面向对象(二)

    7 多态 多态: 多种形态 多态分俩种: 方法重写和方法加载 7.1 方法重写 子类重写了父类的同名的方法 <?php class Person{ public function show(){ ...

  5. Python docs

    刚刚发现了Python好用的文档查询网页:Python docs,可以选择python版本查询,跟OpenCV docs很相似,很好用-

  6. mongodb--linux下的安装

    linux下ubuntu的安装及启动 sudo apt-get upgrade sudo apt-get udpate sudo apt-get install mongodb 用 命令查看一下mon ...

  7. BA--空调系统一次泵和二次泵区别

    通常来说,空调系统是按照满负荷设计的,但实际运行中,满负荷运行的 时间不足 3% ,空调设备绝大部分时间内在远低于额定负荷的情况下运转.在 部分负荷下,虽然冷水机组可以根据实际负荷调节相应的冷量输出, ...

  8. WinSCP介绍、安装、使用

    前言 如果说XManager通过Xshell.Xftp可以很方便的进行远程管理,那么PuTTY显然不能满足我们的需求,所以这也是今天要介绍的另外一个工具-WinSCP. 简介 WinSCP是一个Win ...

  9. redis代码解析-dictionary类型

    dict本质上是为了解决算法中的查找问题(Searching),一般查找问题的解法分为两个大类:一个是基于各种平衡树,一个是基于哈希表. redis中的dict传统的哈希算法类似,它采用某个哈希函数从 ...

  10. Java关键字整理之二

    abstrac和interface 一.抽象类:abstract 抽象类就是为了继承而存在的,如果你定义了一个抽象类,却不去继承它,那么等于白白创建了这个抽象类,因为你不能用它来做任何事情.对于一个父 ...