java IO 入门例子
import java.io.File; /**
* File类型基本操作
*/ /**
* @author Administrator
*
*/
public class FileDemo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final String PATH = "c:\\text.txt";
File file = new File(PATH);
if (file.exists()) {
if (file.isFile()) {
System.out.println("名称:" + file.getName());
System.out.println("相对路径:" + file.getPath());
System.out.println("绝对路径:" + file.getAbsolutePath());
System.out.println("文件大小:" + file.length() + "字节");
} else if (file.isDirectory()) {
System.out.println("这是一个目录!");
}
} else {
System.out.println("文件不存在!");
}
} }
import java.io.FileInputStream;
import java.io.FileOutputStream; /**
* 使用字节流写入文件、读取文件。
*/
/**
* @author Administrator
*
*/
public class ByteStreamDemo { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
final String PATH = "c:\\text.txt"; FileOutputStream fos = null;
try {
String content = "王云鹏是帅哥!";
byte[] contents = content.getBytes();
fos = new FileOutputStream(PATH);
fos.write(contents, 0, contents.length);
fos.flush();
} catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
if (fos != null) {
fos.close();
}
} FileInputStream fis = null;
try {
fis = new FileInputStream(PATH);
int length = fis.available();
byte[] contents = new byte[length];
while(fis.read(contents, 0, length)!=-1){
String content = new String(contents);
System.out.println(content);
}
} catch (Exception e) {
// TODO: handle exception
throw e;
}
finally{
if(fis!=null){
fis.close();
}
}
} }
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream; /**
* 采用二进制流方式写入文件、读取文件。
*/ /**
* @author Administrator
*
*/
public class BinaryStreamDemo2 { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
final String PATH = "c:\\text.txt"; java.io.FileOutputStream fos = null;
java.io.DataOutputStream dos = null;
try {
fos = new FileOutputStream(PATH);
dos = new DataOutputStream(fos); dos.writeInt(1);
dos.writeLong(2);
dos.writeUTF("王云鹏是帅哥!");
dos.flush(); } catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
if (dos != null) {
dos.close();
}
if (fos != null) {
fos.close();
}
} java.io.FileInputStream fis = null;
java.io.DataInputStream dis = null;
try {
fis = new FileInputStream(PATH);
dis = new DataInputStream(fis); int i = dis.readInt();
long l = dis.readLong();
String content = dis.readUTF();
System.out.println(i);
System.out.println(l);
System.out.println(content);
} catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
if (dis != null) {
dis.close();
}
if (fis != null) {
fis.close();
}
}
} }
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream; /**
* 采用二进制流方式写入文件、读取文件。
*/ /**
* @author Administrator
*
*/
public class BinaryStreamDemo { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
java.io.FileOutputStream fos = null;
java.io.DataOutputStream dos = null;
java.io.FileInputStream fis = null;
java.io.DataInputStream dis = null;
try {
fis = new FileInputStream("c:\\source.jpg");
dis = new DataInputStream(fis); fos = new FileOutputStream("c:\\desc.jpg");
dos = new DataOutputStream(fos); int temp = -1;
while((temp=dis.read())!=-1){
dos.write(temp);
}
} catch (Exception e) {
// TODO: handle exception
throw e;
}
finally
{
if(dos!=null)
{
dos.close();
}
if(fos!=null){
fos.close();
}
if(dis!=null){
dis.close();
}
if(fis!=null){
fis.close();
}
}
System.out.println("copy file success!");
} }
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays; /**
* 使用字符流写入文件、读取文件。
*/ /**
* @author Administrator
*
*/
public class CharStreamDemo { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
final String PATH = "c:\\text.txt"; FileWriter fw = null;
try{
fw = new FileWriter(PATH);
fw.write("王云鹏是帅哥!");
fw.flush();
}catch(IOException ex){
throw ex;
}
finally{
if(fw!=null){
fw.close();
}
} FileReader fr = null;
try {
fr = new FileReader(PATH);
char[] chars = new char[1024];
StringBuilder sb = new StringBuilder();
int length = 0;
while((length=fr.read(chars))!=-1){
if(length==1024){
sb.append(chars);
}else
{
char[] less = Arrays.copyOf(chars, length);
sb.append(less);
}
Arrays.fill(chars, ' ');//清空数组
}
System.out.println(sb.toString()); } catch (Exception e) {
// TODO: handle exception
throw e;
}
finally{
if(fr!=null){
fr.close();
}
}
} }
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter; /**
* 使用字符流+缓冲器写入文件、读取文件。
*/ /**
* @author Administrator
*
*/
public class CharBufferStreamDemo { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
final String PATH = "c:\\text.txt"; FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(PATH);
bw = new BufferedWriter(fw);
bw.write("王云鹏是帅哥!");
bw.newLine();
bw.write("王云鹏是北大青鸟趟城中心的帅哥!");
bw.flush();
} catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(PATH);
br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
// TODO: handle exception
throw e;
} finally {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
}
} }
java IO 入门例子的更多相关文章
- Java io 入门
目录 前言 代码演练 字符流 FileReader,FileWriter: BufferedReader,BufferedWriter: InputStreamReader,OutputStreamW ...
- Java IO入门
目录 一. 数据源(流) 二. 数据传输 三. 总结 我们从两个方面来理解Java IO,数据源(流).数据传输,即IO的核心就是对数据源产生的数据进行读写并高效传输的过程. 一. 数据源(流) 数据 ...
- Java IO(十一) DataInputStream 和 DataOutputStream
Java IO(十一) DataInputStream 和 DataOutputStream 一.介绍 DataInputStream 和 DataOutputStream 是数据字节流,分别继承自 ...
- Java api 入门教程 之 JAVA的IO处理
IO是输入和输出的简称,在实际的使用时,输入和输出是有方向的.就像现实中两个人之间借钱一样,例如A借钱给B,相对于A来说是借出,而相对于B来说则是借入.所以在程序中提到输入和输出时,也需要区分清楚是相 ...
- Java入门:Java IO概括
I/O 问题是任何编程语言都无法回避的问题,可以说 I/O 问题是整个人机交互的核心问题,因为 I/O 是机器获取和交换信息的主要渠道.在当今这个数据大爆炸时代,I/O 问题尤其突出,很容易成为一个性 ...
- Java IO学习笔记八:Netty入门
作者:Grey 原文地址:Java IO学习笔记八:Netty入门 多路复用多线程方式还是有点麻烦,Netty帮我们做了封装,大大简化了编码的复杂度,接下来熟悉一下netty的基本使用. Netty+ ...
- java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...
- Java IO使用入门
总体结构 IO应该是平时项目里最常用到的功能了,无论是简单的文件读取,还是服务器端口监听,都会用到IO:但是,在面对Java IO包里庞杂的IO基础类库时,经常会很头大,是该选择InputStream ...
- Java入门系列(十)Java IO
概述 总体而言,java的读写操作又分为两种:字符流和字节流. 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件. 什么是流? ...
随机推荐
- vim/vi 命令详解
在工作中,要对服务器上的文件进行的修改,可以使用ssh远程登录到服务器上,并且使用vi进行快速的编辑即可,在没有图形界面的环境下,要编辑文件,vi是最佳选择! vi命令是Linux中最经典的文本编辑器 ...
- [16] 螺旋面(Spire)图形的生成算法
顶点数据的生成 bool YfBuildSpireVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, Yu ...
- Java主流Web Service框架介绍:CXF和Axis2
CXF和Axis2是目前java平台上最主流的两个框架,虽然两个项目都隶属ASF,但却是基于不同思想和风格实现的,因此也各有所长. CXF:http://cxf.apache.org/ 是由过去的 ...
- BNU Concentric Rings
http://www.bnuoj.com/bnuoj/problem_show.php?pid=16030 Concentric Rings There are several different ...
- RAID5工作原理介绍
RAID 5是一种存储性能.数据安全和存储成本兼顾的存储解决方案.以四个硬盘组成的RAID 5为例,其数据存储方式如图4所示:图中,P0为D0,D1和D2的奇偶校验信息,P1为D3,D4,D5的奇偶校 ...
- 2017.8.30 elasticsearch-sql的安装与使用
参考来自: http://blog.csdn.net/u012307002/article/details/52837756 https://github.com/NLPchina/elasticse ...
- (转)<Unity3D>Unity3D在android下调试
转自:http://blog.csdn.net/zuoyamin/article/details/11827309 一.工具准备 1.JDK——由于android是基于Java平台开发的,jdk是必须 ...
- [Javascript] Understand Curry
The act of currying can be described as taking a multivariate function and turning it into a series ...
- [Node.js]33. Level 7: Persisting Questions
Let's go back to our live-moderation app and add some persistence, first to the questions people ask ...
- 【Java VisualVM】使用 VisualVM 进行性能分析及调优
转载:https://blog.csdn.net/lmb55/article/details/79267277 一.概述 开发大型 Java 应用程序的过程中难免遇到内存泄露.性能瓶颈等问题,比如文件 ...