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. Javascript基础系列之(一)JavaScript语法

    javascript的语法 1.区分大小写 javascript中,变量.函数.运算符都区分大小写. 2.弱类型变量 定义变量只用 "var"关键字 var age = 25; v ...

  2. 每天一个linux命令(52):scp命令

    scp 是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且 scp传输是加密的.可能会稍微影响一下速度.当你服 ...

  3. ZOJ 3201 树形dp+背包(简单题)

    #include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...

  4. BZOJ-4010 菜肴制作 贪心+堆+(拓扑图拓扑序)

    无意做到...char哥还中途强势插入干我...然后据他所言,看了一会题,一转头,我爆了正解....可怕 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec Memory L ...

  5. sqlserver 还原数据库

    1.解决什么问题? a.还原数据库的时候老是提示 不能独占 2.解决方案 ALTER DATABASE [ datebase] SET OFFLINE WITH ROLLBACK IMMEDIATE ...

  6. 轻量级应用开发之(10) UINavigationController导航控制器

    一 多控制器 1)一个iOS的app很少只由一个控制器组成,除非这个app极其简单2)当app中有多个控制器的时候,我们就需要对这些控制器进行管理3)有多个view时,可以用一个大的view去管理1个 ...

  7. IOS 面试

    1. #import , #include的区别 @import防止类的重复引用,#import 确定一个文件只能被导入一次,这使在递归包含中不会出现问题. @class一般用于头文件中需要声明该类的 ...

  8. 蓝桥T291(BFS + 输出路径)

    http://lx.lanqiao.org/problem.page?gpid=T291 学霸的迷宫   时间限制:1.0s   内存限制:256.0MB      问题描述 学霸抢走了大家的作业,班 ...

  9. 巧用jquery实现提交(submit)表单时候验证文本框是否为空

    http://www.cnblogs.com/wifi/articles/2387131.html 先引用jquery Html部分--------------------------------- ...

  10. ASP.NET MVC 过滤器详解

    http://www.fwqtg.net/asp-net-mvc-%E8%BF%87%E6%BB%A4%E5%99%A8%E8%AF%A6%E8%A7%A3.html 我经历了过滤器的苦难,我想到了还 ...