1.文件写入

import java.io.*;

public class MainClass{
public static void main(String[] args){
try{
BufferedWriter out = new BufferedWriter(new FileWriter("testwrite.txt"));
out.write("lalala");
out.close();
System.out.println("创建成功 !");
}catch(IOException e){
e.printStackTrace();
}
}
}

2.读取文件内容:

import java.io.*;

public class MainClass{
public static void main(String[] args){
try{
BufferedReader rd = new BufferedReader(new FileReader("testwrite.txt"));
String str;
while((str=rd.readLine())!=null){
System.out.println(str);
}
rd.close();
System.out.println(str);
}catch(IOException e){
e.printStackTrace();
}
}
}

3.删除文件(File类的delete()方法)

import java.io.*;

public class MainClass{
public static void main(String[] args){
try{
File file = new File("D:/Workspace/sqldemo/testwrite.txt");
if(file.delete()){
System.out.println(file.getName()+"文件已删除");
}else{
System.out.println("文件删除失败");
}
}catch(Exception e){
e.printStackTrace();
}
}
}

4.将文件内容复制取另一个文件

import java.io.*;

public class MainClass{
public static void main(String[] args) throws Exception{
//向srcrile中写入数据
BufferedWriter out1 = new BufferedWriter(new FileWriter("srcfile"));
out1.write("string to be coped\n");
out1.close();
//将数据从srcfile复制到destfile文件中,以1024个字节数据为单位复制
InputStream in = new FileInputStream(new File("srcfile"));
OutputStream out = new FileOutputStream(new File("destfile"));
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf)) >0){
out.write(buf);
}
in.close();
out.close();
//以每行的方式读取destfile文件中数据
BufferedReader in1 = new BufferedReader(new FileReader("destfile"));
String str;
while((str=in1.readLine())!=null){
System.out.println(str);
}
in1.close();
}
}

5.向文件中追加数据

import java.io.*;

public class MainClass{
public static void main(String[] args) throws Exception{
BufferedWriter out = new BufferedWriter(new FileWriter("filename"));
out.write("astring 1\n");
out.close();
//true 追加 FileWriter构造方法
out = new BufferedWriter(new FileWriter("filename",true));
out.write("astring 2");
out.close(); BufferedReader in = new BufferedReader(new FileReader("filename"));
String str;
while((str = in.readLine())!=null){
System.out.println(str);
}
in.close();
}
}

6.创建临时文件

import java.io.*;

public class MainClass{
public static void main(String[] args) throws Exception{
File tmp = File.createTempFile("testtemp",".txt");
System.out.println("文件路径"+tmp.getAbsolutePath());
//终止后删除临时文件
tmp.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(tmp));
out.write("astring");
System.out.println("临时文件已创建:");
out.close();
}
}

7.修改文件最后的修改日期

import java.io.*;
import java.util.Date; public class MainClass{
public static void main(String[] args) throws Exception{
File f = new File("c:/myjava.txt");
f.createNewFile();
//获取文件最后修改日期并打印出来
Date ftime = new Date(f.lastModified());
System.out.println(ftime.toString());
//更改文件最后修改日期并打印出来
System.out.println(f.setLastModified(System.currentTimeMillis()));
ftime = new Date(f.lastModified());
System.out.println(ftime.toString());
}
}

8.获取文件大小 ,使用File类的file.exists()和file.length()方法来获取文件大小

public class MainClass{
public static void main(String[] args) throws Exception{
long size = getSize("c:/install.log");
System.out.println("文件大小 为:"+size);
}
public static long getSize(String filename){
File file = new File(filename);
if(!file.exists()||!file.isFile()){
System.out.println("文件不存在");
return -1;
}
return file.length();
}
}

9.文件重命名

public class MainClass{
public static void main(String[] args) throws Exception{
File oldf = new File("c:/program.txt");
oldf.createNewFile();
File newf = new File("c:/java.txt");
if(oldf.renameTo(newf)){
System.out.println("已重命名");
}else{
System.out.println("error");
}
}
}

10.设置文件只读:f.setReadOnly()

public class MainClass{
public static void main(String[] args) throws Exception{
File f = new File("c:/java.txt");
System.out.println(f.setReadOnly());
System.out.println(f.canWrite());
}
}

11.在指定目录中创建文件

public class MainClass{
public static void main(String[] args) throws Exception{
File dir = new File("c:/");
File f = File.createTempFile("test", ".txt", dir);
System.out.println(f.getPath());
}
}

12.文件路径比较

public class MainClass{
public static void main(String[] args) throws Exception{
File dir1 = new File("c:/temp/test.txt");
File dir2 = new File("C:/java/test.txt");
if(dir1.compareTo(dir2)==0){
System.out.println("路径一致");
}else{
System.out.println("不一致");
}
}
}

Java之旅_高级教程_实例_文件操作的更多相关文章

  1. python教程(八)·文件操作

    由于离高考越来越近,博主打算本篇文章过后,暂停本系列教程的更新,等到高考完后再继续本系列教程,请谅解! 这次我们学习用python操作文件,包括文件的读.写等-- 操作文件第一步--打开文件 要想操作 ...

  2. Java之旅_高级教程_实例_数组

    摘自:http://www.runoob.com/java/java-examples.html 1.数组排序及元素查找 以下实例演示了如何使用sort()方法对Java数组进行排序,及如何使用 bi ...

  3. Java之旅_高级教程_实例_打印图形

    1.打印菱形 public class MainClass{ public static void main(String[] args){ printStar(10); } public stati ...

  4. Java之旅_高级教程_网络编程

    摘自:http://www.runoob.com/java/java-networking.html JAVA网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. j ...

  5. Java之旅_高级教程_数据结构

    摘自:http://www.runoob.com/java/java-data-structures.html 了解即可 Java 数据结构 Java 工具包提供了强大的数据结构.在Java中的数据结 ...

  6. Java之旅_高级教程_多线程编程

    摘自:http://www.runoob.com/java/java-multithreading.html Java 多线程编程 Java 给多线程编程提供了内置的支持.一条线程指的是进程中的一条执 ...

  7. Java之旅_高级教程_URL处理

    摘自 :http://www.runoob.com/java/java-url-processing.html Java URL 处理 URL(Uniform Resource Locator)中文名 ...

  8. java之旅_高级教程_java泛型

    摘自:http://www.runoob.com/java/java-generics.html JAVA泛型 java泛型(generics)是JDK5中引入的新特性,泛型提供了编译时类型安全检测机 ...

  9. Java之旅_高级教程_序列化

    摘自 :http://www.runoob.com/java/java-serialization.html  Java序列化 Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字 ...

随机推荐

  1. 能ping通外网的域名,浏览器不能上网的解决办法

    1,依次尝试了关闭防火墙,关闭杀毒软件,手动设置DNS都没有用. 2,最后通过这个cmd命令搞定,特此记录一下,重置初始化网络环境. netsh winsock reset 补充,上面的命令,重启电脑 ...

  2. 【转】HTML embed标签使用方法和属性详解

    一.基本语法 代码如下: embed src=url 说明:embed可以用来插入各种多媒体,格式可以是 Midi.Wav.AIFF.AU.MP3等等,Netscape及新版的IE 都支持.url为音 ...

  3. java-信息安全(六)-基于RSA理解数字签名示例

    概述 java-信息安全(四)-数据签名.数字证书 java-信息安全(五)-非对称加密算法RSA RSA工具类 使用java-信息安全(五)-非对称加密算法RSA项目中RSACoder 数字签名理解 ...

  4. Python 字符串转JSON; 先装字典在转JSON; json.dumps(d)

    #-*- coding:UTF-8 -*- import os; import json class MysqlUtil(): def __init__(self): pass if __name__ ...

  5. spring事务传播

    http://my.oschina.net/u/1166271/blog?catalog=448293

  6. [Python] 04 - os & sys module

    相当实用的一些API: Ref: https://docs.python.org/3/library/os.html from os import listdir from os.path impor ...

  7. [DLX精确覆盖] hdu 1603 A Puzzling Problem

    题意: 给你n块碎片,这些碎片不能旋转.翻折. 问你能不能用当中的某些块拼出4*4的正方形. 思路: 精确覆盖裸题了 建图就是看看每一个碎片在4*4中能放哪些位置,这个就作为行. 列就是4*4=16个 ...

  8. redis aof和rdb区别

    转自https://blog.csdn.net/m0_38110132/article/details/76906422 1.前言 最近在项目中使用到Redis做缓存,方便多个业务进程之间共享数据.由 ...

  9. docker 怎么下载指定版本的镜像文件

    在使用Docker时我想pull远端仓库的CentOS 6的镜像,但是我直接搜索下载下来的是最新的版本.那我有没有什么办法直接下载Centos6呢? 方法: 直接上 hub.docker.com 查 ...

  10. I - A/B

    要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个T,表示有T组数据. 每组数据有 ...