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();
}
}
} }

最新文章

  1. ubuntu下设置数据库字符集
  2. 最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow
  3. win7 ubuntu 14.04双系统安装
  4. Android系统介绍与框架(转)
  5. Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)
  6. sort方法的使用、随机数的产生
  7. jQuery插件开发的模式和结构
  8. 20160329javaweb之JSP -session入门
  9. android 一个页面内 多个listview的实现
  10. SpringBoot系列一(入门,ORM,Transaction,log4j2等)
  11. idea 安装热部署插件
  12. yii2.0 集成/引入第三方sdk
  13. Winform .NET 利用NPOI导出大数据量的Excel
  14. Activity中满屏和去标题的实现方法
  15. PowerShell 命令行调试指引(转)
  16. cx_Oracle.DatabaseError: DPI-1047
  17. 20145307陈俊达_安卓逆向分析_Xposed的hook技术研究
  18. 使用math.js进行javascript精确计算
  19. Tensorflow一些常用基本概念与函数(二)
  20. Types方法之isSameType-isSuperType-isSubType

热门文章

  1. SystemVerilog Event Scheduling Algorithm
  2. Java 动态向 JTable 中添加数据
  3. 第二十章 springboot + consul(1)
  4. python环境搭建-Pycharm模块安装方法
  5. MySQL语法大全
  6. Substring with Concatenation of All Words leetcode java
  7. C#代码审查工具 StyleCop
  8. windows CMD命令查看局域网内所有主机名及IP
  9. 微博推荐算法学习(Weibo Recommend Algolrithm)
  10. 代码录播:jQueryMobile 实现一个简单的弹出框效果