java文件處理代碼,java編譯java文件

求一段簡(jiǎn)單的java代碼

不知道是否理解對(duì)了你的意思,大概寫了一下:

十余年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站定制,個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于成都企業(yè)網(wǎng)站定制,高端網(wǎng)頁(yè)制作,對(duì)服務(wù)器托管等多個(gè)方面,擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)。

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("處理完畢");

}

/**

* 文本文件轉(zhuǎn)換為指定編碼的字符串

*

* @param file

* 文本文件

* @param encoding

* 編碼類型

* @return 轉(zhuǎn)換后的字符串

* @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();

}

}

// 返回轉(zhuǎn)換結(jié)果

if (writer != null)

return writer.toString();

else

return null;

}

/**

* 將字符串寫入指定文件(當(dāng)指定的父路徑中文件夾不存在時(shí),會(huì)最大限度去創(chuàng)建,以保證保存成功!)

*

* @param res

* 原字符串

* @param filePath

* 文件路徑

* @return 成功標(biāo)記

*/

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]; // 字符緩沖區(qū)

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)讀取文件然后處理數(shù)據(jù)的代碼

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("文件名錯(cuò)誤!");

return;

}

ListString list = new ArrayListString();

File f = new File(filepath);

BufferedReader reader=null;

try {

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

String tempString = null;

// 一次讀入一行,直到讀入null為文件結(jié)束

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的數(shù)據(jù)并處理

直接給你一段實(shí)際可以用的代碼吧

/**

*?讀取一行一行的文件

*?

*?@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;

}

這段代碼實(shí)現(xiàn)了,你傳入文件的路徑,給你讀出一個(gè)String的List,一個(gè)String就是文件的一行,拿到這個(gè)List,你愛怎么處理就怎么處理,就比如你現(xiàn)在這個(gè)需求吧,就把這個(gè)List拿來遍歷

//這個(gè)是去除不含genotype字符串后的數(shù)組

ListString?resultList?=?new?ArrayListString();

for(String?line:filecon){

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

resultList.add(line);

}

}

這樣處理完后就拿到了一個(gè)只有含有 genotype的數(shù)組,然后剩下的就是把這個(gè)數(shù)組寫到一個(gè)文件里面,我給你看一個(gè)把字符串?dāng)?shù)組寫到文件的方法,你可以拿去直接用

/**

*?寫入一行一行的文件

*?@param?lst?要寫入的字符串?dāng)?shù)組

*?@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文件

我來說一下大致的實(shí)現(xiàn)步驟,具體實(shí)現(xiàn)需要你自己去寫了

1.檢索數(shù)據(jù),檢索到的數(shù)據(jù)假定為一個(gè)list

2.你需要自己寫一個(gè)objectToString之類的方法來把檢索到的數(shù)據(jù)轉(zhuǎn)化為一個(gè)String或StringBuffer,就是往各字段間插",",往個(gè)記錄間插"\r\n",如此這類的轉(zhuǎn)換,假定轉(zhuǎn)換好的字符串為strResult.

3.然后用下面的代碼寫在后臺(tái)來控制下載,文件名那里你可以把時(shí)間格式控制好,或者用前臺(tái)傳過來的參數(shù)做名字。

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練習(xí)題求完整代碼

按照題目要求編寫的用javaBean規(guī)范設(shè)計(jì)的學(xué)生類Student的Java程序如下

需要?jiǎng)?chuàng)建user.java.test包,把Student.java文件和Test.java文件放入包中,編譯Student.java文件并且編譯運(yùn)行Test.java文件得到運(yùn)行結(jié)果

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 "學(xué)號(hào):"+no+",姓名:"+name+",成績(jī):"+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("這些學(xué)生的平均分:"+Student.getAvg(sArray));

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

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

}

}

}

網(wǎng)站題目:java文件處理代碼,java編譯java文件
新聞來源:http://muchs.cn/article0/hcjpio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)、微信公眾號(hào)、軟件開發(fā)、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)