Java之旅_高级教程_实例_文件操作
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之旅_高级教程_实例_文件操作的更多相关文章
- python教程(八)·文件操作
由于离高考越来越近,博主打算本篇文章过后,暂停本系列教程的更新,等到高考完后再继续本系列教程,请谅解! 这次我们学习用python操作文件,包括文件的读.写等-- 操作文件第一步--打开文件 要想操作 ...
- Java之旅_高级教程_实例_数组
摘自:http://www.runoob.com/java/java-examples.html 1.数组排序及元素查找 以下实例演示了如何使用sort()方法对Java数组进行排序,及如何使用 bi ...
- Java之旅_高级教程_实例_打印图形
1.打印菱形 public class MainClass{ public static void main(String[] args){ printStar(10); } public stati ...
- Java之旅_高级教程_网络编程
摘自:http://www.runoob.com/java/java-networking.html JAVA网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. j ...
- Java之旅_高级教程_数据结构
摘自:http://www.runoob.com/java/java-data-structures.html 了解即可 Java 数据结构 Java 工具包提供了强大的数据结构.在Java中的数据结 ...
- Java之旅_高级教程_多线程编程
摘自:http://www.runoob.com/java/java-multithreading.html Java 多线程编程 Java 给多线程编程提供了内置的支持.一条线程指的是进程中的一条执 ...
- Java之旅_高级教程_URL处理
摘自 :http://www.runoob.com/java/java-url-processing.html Java URL 处理 URL(Uniform Resource Locator)中文名 ...
- java之旅_高级教程_java泛型
摘自:http://www.runoob.com/java/java-generics.html JAVA泛型 java泛型(generics)是JDK5中引入的新特性,泛型提供了编译时类型安全检测机 ...
- Java之旅_高级教程_序列化
摘自 :http://www.runoob.com/java/java-serialization.html Java序列化 Java提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字 ...
随机推荐
- spring aop 之xml
1.类库 2.aop概念 一个切面可以有多个切点 3.在方法前后进行aop的测试代码 3.1aop.xml <beans xmlns="http://www.springframewo ...
- Mysql字符串字段中是否包含某个字符串,用 find_in_set
有这样一个需求,在Mysql数据库字符串字段(权限)中,有范围在 1 到 N 之间代表不同权限的值,分别被‘,’分开,现在要取出具有某权限的所有成员列表. 创建表: 1 CREATE TABLE ...
- MT7601 AP模式移植
MT7601 的 STA 模式和 AP 模式的驱动,是不一样的. 所以,需要另外移植驱动 驱动源码位置 https://github.com/eywalink/mt7601u 下载之后,先修改 Mak ...
- CMS 01
环境搭建 工具 sublime mysql 5.7, 数据库管理 Navicat django 1.10, django shell (可以用来检查错误) 操作系统, windows 7 搭建 dja ...
- 第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块
第四百零一节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署virtualenv虚拟环境安装,与Python虚拟环境批量安装模块 virtualenv简介 1.安装virtuale ...
- duilib进阶教程 -- 在MFC中使用duilib (1)
由于入门教程的反响还不错,因此Alberl就以直播的形式来写<进阶教程>啦,本教程的前提: 1.请先阅读<仿迅雷播放器教程> 2.要有一定的duilib基础,如果还没,请先阅读 ...
- SpringBoot------单元测试
1.添加测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- react学习笔记(二)编写第一个react组件
继续上一节课的内容,打开App.js:会看到如下代码: import React, { Component } from 'react'; //在此文件中引用React,以及reat的组件类 imp ...
- Flask框架(2)-JinJa2模板
为了把业务逻辑和表现逻辑分开,Flask把表现逻辑移到JinJa2模板,模板是一个包含响应文本的文件.它用占位变量表示动态部分,其具体要从请求上下文才知道. 把真实值替换掉占位变量成为渲染,JinJa ...
- jenkins 使用curl调用时带文件参数
最近在使用jenkins时,需要使用curl去调用,但原有的jenkins中需要有file参数,baidu查询之未找到. 特意记录下 curl -X POST http://localhost:808 ...