网站建设资讯

NEWS

网站建设资讯

java文件处理代码,java编译java文件

求一段简单的java代码

不知道是否理解对了你的意思,大概写了一下:

十余年专注成都网站制作,成都企业网站定制,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于成都企业网站定制,高端网页制作,对服务器托管等多个方面,拥有丰富的网站建设经验。

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.StringReader;

import java.io.StringWriter;

public class FileReadAndWrite {

private static final int DEFAULT_BUFFER_SIZE = 1024;

public static void main(String[] args) {

File file = new File("E:/workspace/FileIOTest/src/a.txt");

String str = file2String(file, "UTF-8");

str = str.replace('d', 'f');

string2File(str,"E:/workspace/FileIOTest/src/b.txt");

System.out.println("处理完毕");

}

/**

* 文本文件转换为指定编码的字符串

*

* @param file

* 文本文件

* @param encoding

* 编码类型

* @return 转换后的字符串

* @throws IOException

*/

public static String file2String(File file, String encoding) {

InputStreamReader reader = null;

StringWriter writer = new StringWriter();

try {

if (encoding == null || "".equals(encoding.trim())) {

reader = new InputStreamReader(new FileInputStream(file),

encoding);

} else {

reader = new InputStreamReader(new FileInputStream(file));

}

// 将输入流写入输出流

char[] buffer = new char[DEFAULT_BUFFER_SIZE];

int n = 0;

while (-1 != (n = reader.read(buffer))) {

writer.write(buffer, 0, n);

}

} catch (Exception e) {

e.printStackTrace();

return null;

} finally {

if (reader != null)

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

// 返回转换结果

if (writer != null)

return writer.toString();

else

return null;

}

/**

* 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)

*

* @param res

* 原字符串

* @param filePath

* 文件路径

* @return 成功标记

*/

public static boolean string2File(String res, String filePath) {

boolean flag = true;

BufferedReader bufferedReader = null;

BufferedWriter bufferedWriter = null;

try {

File distFile = new File(filePath);

if (!distFile.getParentFile().exists())

distFile.getParentFile().mkdirs();

bufferedReader = new BufferedReader(new StringReader(res));

bufferedWriter = new BufferedWriter(new FileWriter(distFile));

char buf[] = new char[1024]; // 字符缓冲区

int len;

while ((len = bufferedReader.read(buf)) != -1) {

bufferedWriter.write(buf, 0, len);

}

bufferedWriter.flush();

bufferedReader.close();

bufferedWriter.close();

} catch (IOException e) {

e.printStackTrace();

flag = false;

return flag;

} finally {

if (bufferedReader != null) {

try {

bufferedReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return flag;

}

}

求mapreduce(JAVA)读取文件然后处理数据的代码

package test;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

public class Test {

public static void main(String[] args) {

Test t = new Test();

t.mapreduce("c:\\a.txt");

}

public void mapreduce(String filepath){

if(filepath==null||"".equals(filepath)){

System.out.println("文件名错误!");

return;

}

ListString list = new ArrayListString();

File f = new File(filepath);

BufferedReader reader=null;

try {

reader = new BufferedReader(new FileReader(f));

String tempString = null;

// 一次读入一行,直到读入null为文件结束

while ((tempString = reader.readLine()) != null) {

list.add(tempString);

}

reader.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (Exception e1) {

}

}

}

for(int i=0,length=list.size();ilength;i+=3){

String name = list.get(i).split(":")[1];

String id = list.get(i+1).split(":")[1];

String address = list.get(i+2).split(":")[1];

System.out.println("name:"+name+",id:"+id+",address:"+address);

}

}

}

java读取txt的数据并处理

直接给你一段实际可以用的代码吧

/**

* 读取一行一行的文件

* @param filename

* @return

* @throws IOException

*/

public static ListString readLinedFile(String filename){

ListString filecon = new ArrayListString();

String m = "";

BufferedReader file = null;

try{

file = new BufferedReader(new FileReader(filename));

while ((m = file.readLine()) != null) {

if (!m.equals("")) // 不需要读取空行

{

filecon.add(m);

}

}

file.close();

}catch(IOException e){

e.printStackTrace();

}

return filecon;

}

这段代码实现了,你传入文件的路径,给你读出一个String的List,一个String就是文件的一行,拿到这个List,你爱怎么处理就怎么处理,就比如你现在这个需求吧,就把这个List拿来遍历

//这个是去除不含genotype字符串后的数组

ListString resultList = new ArrayListString();

for(String line:filecon){

if(line.indexOf("genotype") != -1){

resultList.add(line);

}

}

这样处理完后就拿到了一个只有含有 genotype的数组,然后剩下的就是把这个数组写到一个文件里面,我给你看一个把字符串数组写到文件的方法,你可以拿去直接用

/**

* 写入一行一行的文件

* @param lst 要写入的字符串数组

* @param filename 要写入的文件路径

* @return

* @throws IOException

*/

public static void writeLinedFile(List lst, String filePath) throws IOException {

File file = new File(filePath);

BufferedWriter out = new BufferedWriter(new FileWriter(file, false));

for (int i = 0; i  lst.size(); i++) {

String temp = (String) lst.get(i);

if (!StringUtils.isBlank(temp)) {

out.write(temp);

out.newLine();

}

}

out.close();

out = null;

file=null;

}

这样懂了吧?

java处理csv文件

我来说一下大致的实现步骤,具体实现需要你自己去写了

1.检索数据,检索到的数据假定为一个list

2.你需要自己写一个objectToString之类的方法来把检索到的数据转化为一个String或StringBuffer,就是往各字段间插",",往个记录间插"\r\n",如此这类的转换,假定转换好的字符串为strResult.

3.然后用下面的代码写在后台来控制下载,文件名那里你可以把时间格式控制好,或者用前台传过来的参数做名字。

response.setContentType("application/download;charset=UTF-8");

response.setHeader("Content-disposition","attachment;filename=\"" +new Date()+".csv\"");

OutputStream o = response.getOutputStream();

byte b[] = strResult.getBytes();

try{

o.write(b);

}catch(IOException e){

e.printStackTrace();

}finally{

o.close();

}

java练习题求完整代码

按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下

需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果

Student.java文件代码如下

package user.java.test;

import java.io.Serializable;

public class Student implements Serializable{

private static final long serialVersionUID = 1L;

private String no;

private String name;

private double score;

public Student(){}

public Student(String no,String name,double score){

this.no=no;

this.name=name;

this.score=score;

}

public String getNo(){ return no;}

public void setNo(String no){ this.no=no;}

public String getName(){ return name;}

public void setName(String name){ this.name=name;}

public double getScore(){ return score;}

public void setScore(double score){ this.score=score;}

public String toString(){

return "学号:"+no+",姓名:"+name+",成绩:"+score;

}

public static double getAvg(Student[] sArray){

double sum=0,avg;

for(int i=0;isArray.length;i++){

sum=sum+sArray[i].getScore();

}

avg=sum/sArray.length;

return avg;

}

}

Test.java文件代码如下

package user.java.test;

public class Test{

public static void main(String[] args){

Student[] sArray=new Student[5];

sArray[0]=new Student("001","张三",89.5);

sArray[1]=new Student("002","李四",82.5);

sArray[2]=new Student("003","王五",93);

sArray[3]=new Student("004","赵六",73.5);

sArray[4]=new Student("005","孙七",66);

System.out.println("这些学生的平均分:"+Student.getAvg(sArray));

for(int i=0;isArray.length;i++){

System.out.println(sArray[i].toString());

}

}

}


本文题目:java文件处理代码,java编译java文件
浏览地址:http://cdweb.net/article/hcjpio.html