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. 获取listview的高度代码

    public int getTotalHeightofListView(ListView listView) { ListAdapter mAdapter = (ListAdapter) listVi ...

  2. python爬虫:读取PDF

    下面的代码可以实现用python读取PDF,包括读取本地和网络上的PDF. pdfminer下载地址:https://pypi.python.org/packages/source/p/pdfmine ...

  3. 将mysql默认编码改为UTF8

    windows: a. WIN+R  net stop mysql  关闭mysql服务 b. 复制my-dafault.ini,重命名为my.ini,进入里面 UBUNTU: ** sudo vim ...

  4. JQuery学习笔记系列(一)----选择器详解

    笔者好长时间没有更新过博客园的笔记了,一部分原因是去年刚刚开始工作一段时间忙碌的加班,体会了一种每天加班到凌晨的充实感,之后闲暇时间了也因为自己懒惰没有坚持记笔记的习惯,现在重新拾起来. 借用古人的一 ...

  5. 项目随笔@Service("testService")-------第二篇

    在springmvc中使用注解已经司空见惯了,今天见到了@Service("xxx")这种形式,让我大吃一惊.原来在service后面可以加参数,作为该service的名字,在sp ...

  6. 【转】Oralce基础—Sqlplus工具运用 礼记八目 2017-12-20 20:22:45

    原文地址:https://www.toutiao.com/i6501603661565657614/ 一.数据库连接: sqlplus [user_name[/password][@ host_str ...

  7. vc++绘图,颜色

    新建mfc应用程序,Graphic ,单文档 添加菜单项,点,直线,矩形,椭圆 建立类导向 MFC ClassWizard,为菜单项添加命令响应 添加成员变量 在CGraphicView构造函数中进行 ...

  8. 对服务器磁盘、CPU、内存使用状态,设置163邮件告警

    1,桥接模式可上网,首先你的邮箱已经开通yum -y install mailx dos2unix.x86_64  mailx -V[root@localhost ~]# vim /etc/mail. ...

  9. 安装和启动Elasticseach

    1.在windows上安装和启动Elasticseach 1.安装JDK,至少1.8.0_73以上版本,java -version2.下载和解压缩Elasticsearch安装包.Elasticsea ...

  10. kali 安装nessus

    下载home版: http://www.tenable.com/products/nessus/select-your-operating-system#tos 获取激活码:http://www.te ...