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提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字 ...
随机推荐
- 如何进行 iPhone 客户端的软件测试
如何进行 iPhone 客户端的软件测试客户端版APP主要是通过苹果的APP Store来进行安装的.在测试时,开发会先在本地苹果机上打好包,然后我们在Xcode上进行安装或者直接在开发提供的网址上下 ...
- Vue.js常用指令:v-model
一.v-model指令 v-model 用来获取表单元素的值.对应input输入框获取的是输入的值,单选按钮.复选框.下拉框获取的是选择的状态. 代码示例如下: <!DOCTYPE html&g ...
- Ubuntu 设置NAT共享网络(命令行方法)
本文介绍如何使用iptables来实现NAT转发,事实上就是将一台机器作为网关(gateway)来使用.我们假设充当网关的机器至少有网卡eth0和eth1,使用eth0表示连接到外网的网卡,使用eth ...
- 【Mac brew】代理安装brew insall
http_proxy=dev-proxy.**.**:8080 https_proxy=dev-proxy.**.**:8080 brew install npm
- Python 函数 (关键字参数)
关键字参数 可变参数允许你传入0个或者任意个参数,这些可变参数在函数调用时会自动组装成一个tuple,而关键字参数允许你传入0个或者任意个含参数名的参数,这些参数在函数内部自动组装成为一个dict d ...
- 除了/etc/init.d/加启动脚本 或者在/etc/rc.local中加启动命令,还可以通过crontab来完成服务器重启后自动启动服务的操作
@reboot /bin/sh /opt/soft/percona/bin/mysqld_safe --defaults-file=/mnt/perconadata/my.cnf --basedir= ...
- C#利用反射实现两个类的对象之间相同属性的值的复制
http://blog.csdn.net/u013093547/article/details/53584591 今天在拷贝对象的时候,看着代码实在是有点烦,一堆一样的代码,还是找找有没有直接反射拷贝 ...
- 利用media query让背景图适应不同分辨率的设备
随着上网方式的多样化,用户选择上网的工具不再仅是PC,而可以是手机,或者平板电脑.随之而来的问题是如何让网页适应不同分辨率,这给前端工程师们带来了新的挑战,其中重要的一点是如何让图片能在不同的分辨率下 ...
- C#自定义Winform无边框窗体
C#自定义Winform无边框窗体 在实际项目中,WinForm窗体或者控件不能满足要求,所以就需要自己设计窗体等,当然设计界面可以用的东西很多,例如WPF.或者一些第三方的库等.本例中将采用WinF ...
- AJAX返回总是ERROR或是没有数据的问题
如果总是到ERROR,是因为async没有定义为false,设置为同步,数据类型要设置为text,不要用json. 示例: if (IDcard != "") { $.ajax({ ...