Java io 操作
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 操作的更多相关文章
- JAVA—IO操作
一.JAVA I/O 输入输出流 1:编码问题 2:File类的使用 3:RandomAccessFile的使用 4:字节流的使用 5:字符流的使用 6:对象的序列化和反序列化 2: file类的使用 ...
- java IO操作:FileInputStream,FileOutputStream,FileReader,FileWriter实例
FileInputStream <span style="font-family:Verdana;">import java.io.File; import java. ...
- Java IO操作——数据操作流DataOutputStream和DataInputStream的使用
学习目标 掌握DataOutputStream和DataInputStream的作用 可以使用DataOutputStream和DataInputStream写入和读入数据 数据操作流 在io包中, ...
- Java IO操作:合并流
合并流:把两个文件合并在一起. 主要操作的是内容. 定义: public class SequenceInputStreamextends InputStream 方法摘要: 操作步骤: 1,分别建立 ...
- JAVA IO操作:数据操作流:DataOutputStream和DataInputStream
掌握DataOutputStream和DataInputStream的作用. 可以使用DataOutputStream和DataInputStream写入和读取数据. 在IO包中提供了两个与平台无关的 ...
- java IO操作,看完你应该就清晰了。
前言: java中IO里的一些知识对于一个java新手来说,是比较难理解的.因为里面存在一些很绕的概念,比如: 1.到底是读入写出,还是读出写入: 2.我要将一个文件的内容拷贝到另一个文件是先用Inp ...
- java IO操作和计算操作:工作内存和主内存 volatile关键字作用;原子操作对象AtomicInteger ....
应该停止但无法停止的计算线程 如下线程示例,线程实例中while循环中的条件,在主线程中通过调用实例方法更新后,while循环并没有更新判断变量是否还成立.而是陷入了while(true)死循环. i ...
- Java IO操作
转自:http://www.cnblogs.com/jyan/articles/2505791.html Johnny Yan的博客 1 InputStream类型 InputStream的作用是标志 ...
- 每天进步一点点-Java IO操作-Java Serializable(对象序列化)的理解和总结
往硬盘文件里写数据 序列化:序列化是将对象转换为容易传输的格式的过程.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.在另一端,反序列化将从该流重 ...
随机推荐
- WinRAR 5.60 无广告正式版
首先明确WinRAR唯一的官网是这个 https://www.rarlab.com/ 其余的都不要相信. 现在的问题是:不要脸的中国代理强行捆绑广告:即使你花钱注册同样要面对弹窗广告!这就不可接受了! ...
- Embeding如何理解?
参考: http://www.sohu.com/a/206922947_390227 https://zhuanlan.zhihu.com/p/27830489 https://www.jianshu ...
- 主从同步工作过程?(binlog日志)
在从数据库服务器的/var/lib/mysql/master.info 记录连接主数据库服务器信息文件mail-relay-bin.XXXXXX 中继日志文件(记录SQL)mail-relay ...
- JS棋盘
有一个棋盘,有64个方格,在第一个方格里面放1粒芝麻重量是0.00001kg, 第二个里面放2粒,第三个里面放4,棋盘上放的所有芝麻的重量 <!DOCTYPE html> <html ...
- 移动端开发-Day1
什么是Node? 它是一个基于Chrome v8引擎的js运行环境,采用高效轻量级的事件驱动,非阻塞式的I/O模型. 非阻塞I/O? 例如,当程序运行到某一函数时,调用后立即返回,不需要等待函数执行完 ...
- PySimpleGUI 的第一个桌面软件
最近学习了python风格的GUI模块,寻思着既然学了,就要学以致用,花了点时间,写了下面这个 简历软件.后面,可以通过接口 获取提交的数据,传到数据库里与网页交互.此软件我已经打包成 exe软件,直 ...
- Nginx+keepalived双机热备(默认路径安装)- 基础篇
负载均衡技术对于一个网站尤其是大型网站的web服务器集群来说是至关重要的!做好负载均衡架构,可以实现故障转移和高可用环境,避免单点故障,保证网站健康持续运行.关于负载均衡介绍,可以参考:linux负载 ...
- CentOS 笔记(二) 端口占用,进程查看
①查看当前端口情况 netstat -nultp ②查看当前进程情况 ps -ef ps -ef|grep dotnet ③强制kill 进程 kill -9 [PIN]
- IIS部署ASP.NET网站后提示只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态...
今天,在IIS上部署网站后,出现了下面错误: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态.还请确保在应用程序配置的 <sy ...
- 开启WIFI
C:\Windows\system32>netsh wlan set hostednetwork mode=allow ssid=wuyechun-wifi k ey= 承载网络模式已设置为允许 ...