Java--读写文件综合
package javatest; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date; class fileTest
{
public static void main(String[] args)
{
Date dt = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String s = df.format(dt);
System.out.println(s);
String path = "C:\\Users\\hp\\Desktop\\test.txt";
//readFile(path);
/*writeTxtFile("C:\\Users\\hp\\Desktop\\test2.txt.",
"C:\\Users\\hp\\Desktop\\test.txt");*/
writeBinaryFile("C:\\Users\\hp\\Desktop\\c.jpg.",
"C:\\Users\\hp\\Desktop\\a.jpg"); } //读取文本文件
public static void readFile(String filePath)
{
File file = new File(filePath);
if (!file.exists())
{
System.out.println("No such file");
}
else
{
try
{
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
// StringBuffer sb=new StringBuffer();
while ((line = reader.readLine()) != null)
{
System.out.println(line);
// sb.append(line);
}
reader.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} //复制文本文件
public static void writeTxtFile(String destFile, String srcFile)
{
try
{
File file = new File(destFile);
if (!file.exists())
{
System.out.println("No such file");
file.createNewFile();
}
else
{
BufferedReader reader = new BufferedReader(new FileReader(
new File(srcFile)));
String line = null; FileOutputStream out = new FileOutputStream(file);
while ((line = reader.readLine()) != null)
{
out.write(line.getBytes());
}
System.out.println("Copyed");
out.close();
reader.close();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} } //复制二进制文件,如图片等
public static void writeBinaryFile(String destFile, String srcFile){
try
{
File file = new File(destFile);
if (file.exists())
{
System.out.println("File already exists,stop writting!");
}
else
{
System.out.println("Creating new file...");
file.createNewFile();
FileInputStream fin = new FileInputStream(new File(srcFile));
byte[]buff=new byte[2014]; FileOutputStream fout = new FileOutputStream(file);
while((fin.read(buff))!= -1)
{
fout.write(buff);
}
System.out.println("Copyed");
fout.close();
fin.close();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Java--读写文件综合的更多相关文章
- Java读写文件方法总结
Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- 【java】java 读写文件
场景:JDK8 将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...
- 转:Java读写文件各种方法及性能比较
干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...
- Java读写文件常用方法
一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...
- 161012、JAVA读写文件,如何避免中文乱码
1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...
- java 读写文件乱码问题
这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...
- java读写文件小心缓存数组
一般我们读写文件的时候都是这么写的,看着没问题哈. public static void main(String[] args) throws Exception { FileInputStr ...
- java读写文件
对于任何文件,不管有没有扩展名,都可以读写.切记,最后要.close();,否则效果出不来. 读操作: package com.wjy.read; import java.io.BufferedRea ...
随机推荐
- JavaScript基础---语言基础(1)
写在前面: 通过四篇博客把JS基础中的基础整理一下,方便自己查阅,这些内容对于实际项目开发中也许并不会在意,但是作为JS的语言基础,自觉还是应该熟悉.在完成这三篇博客(JavaScript基础---语 ...
- 第十六章:脚本化HTTP
写在本章内容前: 第十五章:事件处理 涉及到到较多的文字篇幅,介于个人精力问题,暂不更新.主要包含的内容有事件类型.注册事件处理程序.事件处理程序的调用.文档加载事件.鼠标事件.鼠标滚轮事件.拖放事件 ...
- CreateCompatibleDC与BitBlt 学习
CreateCompatibleDC与BitBlt CreateCompatibleDC 创建一个与指定设备一致的内存设备描述表. HDC CreateCompatibleDC(HDC hdc //设 ...
- ssh开发流程
- JS模式:简单的图书馆享元模式
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- Tarjan算法
SCC即强连通分量,即一个图的子图,其中的点能相互到达,全称是strongly connected component. Tarjan算法是用来找出图的SCC. 伪代码 int index = 0; ...
- 【XDU1144】合并模板
问题 Fate 有 n 个 ACM/ICPC 比赛的模板,每个都是一个独立的 PDF 文件.为了便于打印,万神希望将这些模板合并成一个 PDF 文件.万神有一个工具,可以将至多 k 个 PDF 文件合 ...
- 【CodeForces 602A】C - 特别水的题3-Two Bases
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/C Description After seeing the ...
- GitHub 优秀的 Android 开源项目
转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...
- Linux mount/unmount命令(转)
格式:mount [-参数] [设备名称] [挂载点] 其中常用的参数有:-a 安装在/etc/fstab文件中类出的所有文件系统.-f 伪装mount,作出检查设备和目录的样子,但并不真正挂载文件系 ...