FileOperation
来源:互联网 发布:开淘宝网店安全不 编辑:程序博客网 时间:2024/05/01 21:01
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
public class FileOperation {
/**
* 删除文件
* @param filePathAndName String 文件路径及名称 如c:/abc.txt
* @param fileContent String
* @return boolean
*/
public static void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
}
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
}
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:
* @param newPath String 复制后路径 如:
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
java.io.File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}
/**
*
* @param username
* @param password
* @param savepath
*/
public static void writeFile(String username, String password,String savepath,String sex,String age,String email,String resume,String alias) {
String pathandfileName=savepath+"/"+username;
File file = new File(pathandfileName);
BufferedWriter writer = null;
try {
System.out.println("以行为单位读取文件内容,一次写一整行:");
writer = new java.io.BufferedWriter(new java.io.FileWriter(file));
writer.write("[Main]");
writer.newLine();
writer.write("Alias="+alias);
writer.newLine();
writer.write("Member=0");
writer.newLine();
writer.write("Credit=0");
writer.newLine();
writer.write("Grade=0");
writer.newLine();
writer.write("Age="+age);
writer.newLine();
writer.write("Sex="+sex);
writer.newLine();
writer.write("Password="+password);
writer.newLine();
writer.write("Email="+email);
writer.newLine();
writer.write("Oicq=");
writer.newLine();
writer.write("Icon=");
writer.newLine();
writer.write("Photo=http://");
writer.newLine();
writer.write("Resume="+resume);
writer.newLine();
writer.write("Question=我的生日");
writer.newLine();
writer.write("Answer=");
writer.newLine();
writer.write("Status=1");
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null){
try {
writer.close();
} catch (IOException e1) {
}
}
}
}
/**
*
* @param username
* @param password
* @param savepath
*/
public static void modifyFile(String username, String password,String savepath,String age,String sex,String email,String resume,String alias) {
String pathandfileName=savepath+"/"+username;
String temp="";
try{
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File(pathandfileName)));
byte b[]=new byte[1024*1024];
int length=bis.read(b);
temp=new String(b,0,length);
bis.close();
String str[]=temp.split("/n");
for(int i=0;i<str.length;i++){
if(i==1)//
{
str[1]="Alias="+alias;
}
if(i==5)//
{
str[5]="Age="+age;
}
if(i==6)//
{
str[6]="Sex="+sex;
}
if(i==7)//
{
str[7]="Password="+password;
}
if(i==8)//
{
str[8]="Email="+email;
}
if(i==12)//
{
str[12]="Resume="+resume;
}
System.out.print(str[i]);
}
BufferedWriter writer = null;
writer = new java.io.BufferedWriter(new java.io.FileWriter(pathandfileName));
for(int i=0;i<str.length;i++)
{
writer.write(str[i]);
writer.newLine();
}
writer.close();
}catch(Exception e)
{
}
}
/**
*
* @param username
* @param password
* @param savepath
*/
public static void readFile(String username, String password,String savepath) {
String pathandfileName=savepath+"/"+username;
File file = new File(pathandfileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null){
//显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null){
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
}
- FileOperation
- FileOperation
- ERROR [FileOperation] java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
- 哥做的不是网站 是寂寞
- Mina框架中的filter (一)解码器
- 单态
- http://www.fa.omron.com.cn/support/faqview.jsp?pid=0&id=3351&cid=null
- JavaScript实现,判断一个点是否在多边形内
- FileOperation
- 【ZT】一些正则
- 文件批量搜索替换工具比较
- spring 注意事项
- DataTable.Compute的用法
- 实用JS代码
- Flex用BitmapData实现文本旋转
- asp.net获取当前时间的所有方法整理
- 使用QuickPart开发WebPart