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 ...
随机推荐
- 转-JS子窗口创建父窗口操作父窗口
Javascript弹出子窗口 可以通过多种方式实现,下面介绍几种方法 (1) 通过window对象的open()方法,open()方法将会产生一个新的window窗口对象 其用法为: window ...
- Spring对Hibernate的session生效期(事物提交管理)介绍
在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据操作,然后提交事务,关闭事务,这样做的原因是因为Hibernate默认的事务自动提交是false,它是需要人为的手动 ...
- 【Gym 100685J】Just Another Disney Problem(交互/排序)
第一次做交互题. 题意是有n个数(n<1000),你通过问1 a b,后台返回你YES代表a<b,NO代表a>b.要你在10000次询问内给出一个符合的排列.n=1000来说,100 ...
- Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析
上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种 ...
- matlab学习笔记 bsxfun函数
matlab学习笔记 bsxfun函数 最近总是遇到 bsxfun这个函数,前几次因为无关紧要只是大概看了一下函数体去对比结果,今天再一次遇见了这个函数,想想还是有必要掌握的,遂查了些资料总结如下. ...
- 使用 PHP 和 Apache Solr 实现企业搜索
原文链接:http://www.ibm.com/developerworks/cn/opensource/os-php-apachesolr/ http://blog.csdn.net/hzcyc ...
- Linux下安装php screw
1.下载地址:http://sourceforge.net/projects/php-screw/ 2. tar zxvf php_screw_1.5.tar.gz cd php_screw_1.5 ...
- struts+spring action应配置为scope="prototype"
truts+spring action应配置为scope="prototype" <bean id="personAction" scope=" ...
- 通过开源程序同时解决DNS劫持和DNS污染的问题
我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...
- iOS学习笔记—ViewController/生命周期
ViewController是iOS应用程序中重要的部分,是应用程序数据和视图之间的重要桥梁,ViewController管理应用中的众多视图.iOS的SDK中提供很多原生ViewController ...