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. 教你一招:windows批处理中实现延时的办法

    五种方法可以实现批出里的延时,推荐使用方法一,该方法也是使用最多的. 方法一 用ping命令延迟(这是最简单而且是最常见的): @echo off echo "use ping to del ...

  2. 教你一招:解决Win 10安装软件时提示:文件系统错误 (-1073740940)

    1.win+R输入 gpedit.msc 2.左边计算机配置 windows设置——安全设置——本地策略——安全选项 3.在安全选项右边选择 用户账户控制:管理员批准模式中管理员的提升权限提示的行为, ...

  3. Android 进程保活招式大全(转载)

    目前市面上的应用,貌似除了微信和手Q都会比较担心被用户或者系统(厂商)杀死问题.本文对 Android 进程拉活进行一个总结. Android 进程拉活包括两个层面: A. 提供进程优先级,降低进程被 ...

  4. Best Practices in Asynchronous Programming

    http://blog.stephencleary.com/ http://blogs.msdn.com/b/pfxteam/

  5. IDEA VS 常用高效 黄金 快捷键

    [参考] VS 常用高效 快捷键 身为一个编程人员,掌握IDE的快键是提高开发效率最简单直接的方法,也是必备技能.和网上的大篇罗列不同,下面只讲精髓,根据实践不断调整.本人C#转Java,曾经试过Ec ...

  6. mysql+redis

    微博的系统架构,想用mysql+redis配合使用,具体操作步骤: 写入数据到Redis,,然后在写个运行cron的脚本,美妙读内存,并写入数据库即可. 使用注意: 1.MySQL使用需要注意的地方: ...

  7. python加快数据处理的方法

    1.一切数据库操作最好使用内网连接, 2.使用批量操作接口操作数据库,而不是多线程频繁操作单条数据 3.如果python进程的cpu使用率达到100%了,需要开启多进程.java单个进程cpu使用率在 ...

  8. [Bayes] Improve HMM step by step

    以下是HMM,当emission probability变为高斯时,只需改变其中相关部分即可,也就是下图最后一行. 如下可见,在优化过程中套路没有太大的影响,但变为高斯后表达变得更精确了呢. 当然,这 ...

  9. C# 如何提取字符串中的数字

    下面讲解如何在字符串当中抓取到数字 方法一.使用正则表达式 1.纯数字提取 string str = "提取123abc提取"; //我们抓取当前字符当中的123 string r ...

  10. cube-ui修改按钮颜色

    首先,当我们按照脚手架一步一步创建完项目以后 $ vue init cube-ui/cube-template projectname $ sudo npm install $ npm start 主 ...