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--读写文件综合的更多相关文章

  1. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  2. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  3. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  4. 【java】java 读写文件

    场景:JDK8  将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...

  5. 转:Java读写文件各种方法及性能比较

    干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...

  6. Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

  7. 161012、JAVA读写文件,如何避免中文乱码

    1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...

  8. java 读写文件乱码问题

    这样写,会出现乱码.原因是文件时gbk格式的, BufferedReader br = new BufferedReader(new FileReader(indir)); BufferedWrite ...

  9. java读写文件小心缓存数组

    一般我们读写文件的时候都是这么写的,看着没问题哈.   public static void main(String[] args) throws Exception {   FileInputStr ...

  10. java读写文件

    对于任何文件,不管有没有扩展名,都可以读写.切记,最后要.close();,否则效果出不来. 读操作: package com.wjy.read; import java.io.BufferedRea ...

随机推荐

  1. 第二十二课:js事件原理以及addEvent.js的详解

    再看这篇博客之前,希望你已经对js高级程序编程一书中的事件模块进行了详读,不然我只能呵呵了. document.createEventObject,在IE下创建事件对象event. elem.fire ...

  2. ibatis中的$和#的区别

    介绍 在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型匹配,而$不进行数据类型匹配,例如: select * fr ...

  3. Java Web中将oracle的数据库内容以表格形式展现到页面中(分页展示)

    分页SQL语句: ----分页显示 select * from (select rownum as r,t.* from () ; 查询的结果如下: 这个SQL,使用了三层嵌套的查询方式: 1)最内层 ...

  4. Java编程思想学习(十二) 数组和容器

    一.数组 1).数组的多种初始化方式 下面总结了初始化数组的多种方式,以及如何对指向数组的引用赋值,使其指向另一个数组对象.值得注意的是:对象数组和普通数组的各种操作基本上都是一样的:要说有什么不同的 ...

  5. 【BZOJ-3293&1465&1045】分金币&糖果传递×2 中位数 + 乱搞

    3293: [Cqoi2011]分金币 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 854  Solved: 476[Submit][Status] ...

  6. BZOJ-1491 社交网络 FLoyd+乱搞

    感觉这两天一直在做乱搞的题... 1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1279 Solved: 732 ...

  7. 【bzoj2823】 AHOI2012—信号塔

    http://www.lydsy.com/JudgeOnline/problem.php?id=2823 (题目链接) 题意 求最小圆覆盖 Solution 关于最小圆覆盖的做法,论文里面都有.其实真 ...

  8. document.body.scrollTop 各浏览器兼容性解决

    document.compatMode:获取当前浏览器采用的渲染方式.主要是浏览器的模式,有两个:BackCompat,CSS1Compat.其中前者是怪异模式,后者是标准模式. IE默认是BackC ...

  9. Linux Kernel Synchronization && Mutual Exclusion、Linux Kernel Lock Mechanism Summarize

    目录 . 内核锁机制 . 同步与互斥 . 锁定内存总线原子操作 . 信号量 . 自旋锁 . RCU机制 . PERCPU变量 . 内存和优化屏障 . 读者/写者锁 . 大内核锁 . 互斥量 1. 内核 ...

  10. myeclipse 部署应用

    昨天把MyEclipse10给安装上了,今天想在MyEclipse下启动Tomcat并在浏览器中看到写的Web页面,但是当在浏览器中输入地址时,出现了404错误,出现这个错误的原因是因为没有找到指定的 ...