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 ...
随机推荐
- Symfony启动过程详细学习
想了解symfony的启动过程,必须从启动文件(这里就以开发者模式)开始. <?php /* * web/app_dev.php */ $loader = require_once __DIR_ ...
- 多个TableView的练习
效果图: 左边图片的代码: // // SecViewController.m // UI__多个TableView练习 // // Created by dllo on 16/3/17. // Co ...
- 深入理解web项目的配置文件
1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...
- simple-LDAP-auth / ldap_auth.php
<?php /** * simple class for LDAP authentification * Copyright (C) 2013 Petr Palas This program i ...
- Oracle自定义函数
核心提示:函数用于返回特定数据.执行时得找一个变量接收函数的返回值; 语法如下: create or replace function function_name ( argu1 [mode1] da ...
- 【bzoj1202】 HNOI2005—狡猾的商人
http://www.lydsy.com/JudgeOnline/problem.php?id=1202 (题目链接) 题意 给出m段区间和,判断是否存在某段区间与之前读入的区间相矛盾. Soluti ...
- BZOJ2460 [BeiJing2011]元素
Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石. 一般地,矿石越多则法力越 ...
- hihocoder #1270 建造基地
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...
- log4j使用教程详解(怎么使用log4j2)
1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以了(xx是乱七八糟的版本号): log4j-core-xx.jar log4j-api-xx.jar 2. 导入到 ...
- 图片服务器和WEB应用服务器相分离的简单方案
只是简单说明一下原理,其它的自己探索吧:) 一.两个域名:www.domain.com和img.domain.com 二.在www域名的服务器中上传文件: up.html <form name= ...