java档案 *** 作怎样从档案中读出资料然后写入到另外一个档案中

java档案 *** 作怎样从档案中读出资料然后写入到另外一个档案中,第1张

java档案 *** 作怎样从档案中读出资料然后写入到另外一个档案中 import javaioBufferedInputStream;

import javaioBufferedOutputStream;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioIOException;

public class Usebuffer

{

public static void main(String args[]) throws IOException

{

FileInputStream fis;

FileOutputStream fos;

BufferedInputStream bis;

BufferedOutputStream bos;

int i;

try

{

fis=new FileInputStream("text1txt"); 档案输入流

bis=new BufferedInputStream(fis); 连线带缓冲的输入流

fos=new FileOutputStream("text2txt"); 档案输出流

bos=new BufferedOutputStream(fos); 连线带缓冲的输出流

i=bisread(); 读资料

while(i!=-1)

{

boswrite(i); 写资料

bosflush(); 强制输出

i=bisread();

}

fisclose();

fosclose();

bisclose();

bosclose();

}

catch(IOException e)

{

Systemoutprintln("do not find the file");

}

}

}

参考

java中怎么从档案中读出资料到JTabel

java中有四种将档案的内容读取成字串

方式一:

Java code

/

以位元组为单位读取档案,常用于读二进位制档案,如、声音、影像等档案。

当然也是可以读字串的。

/

/ 貌似是说网路环境中比较复杂,每次传过来的字元是定长的,用这种方式?/

public String readString1()

{

try

{

FileInputStream 用于读取诸如影象资料之类的原始位元组流。要读取字元流,请考虑使用 FileReader。

FileInputStream inStream=thisopenFileInput(FILE_NAME);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] buffer=new byte[1024];

int length=-1;

while( (length = inStreamread(buffer) != -1)

{

boswrite(buffer,0,length);

write方法 SDK 的解释是 Writes count bytes from the byte array buffer starting at offset index to this stream

当流关闭以后内容依然存在

}

bosclose();

inStreamclose();

return bostoString();

为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"UTF-8") 不更好么

return new String(bostoByteArray(),"UTF-8");

}

}

求多执行绪读取一个档案,然后写到另外一个档案中的Java实现。

这个是我写的三个类,用于多执行绪 *** 作读取档案内容和写入档案内容,不知道是不是你合你味口。

________________第一个类______读取内容__写入内容____________________

package pro;

import javaio;

public class ReadFileToWriteOtherFile {

private File oldFile;

private File newFile;

private BufferedReader br;

private BufferedWriter bw;

private String totalString="";

private Boolean flag=true; 用于标记档名是否存在 true表示存在

public ReadFileToWriteOtherFile()

{

oldFile=null;

newFile=null;

br=null;

bw=null;

Systemoutprintln("初始化成功");

}

public void readInfoFromFile(String fileName)

{

Systemoutprintln("开始读取");

try

{

oldFile=new File(fileName);

if(oldFileexists()) 如果档案存在

{

Systemoutprintln("存在");

br=new BufferedReader(new FileReader(oldFile));

String info=brreadLine(); 读取一行

while(info!=null)

{

totalString+=info; 将读取到的一行新增到totalString中

info=brreadLine(); 再读取下一行

Systemoutprintln(totalString);

}

Systemoutprintln("读取完成,准备写入…………");

}

else 如果档案不存在

{

Systemoutprintln("档案不存在");

flag=false; 标记该档案不存在

}

Systemoutprintln("totalString="+totalString);

}

catch(FileNotFoundException e)

{

Systemoutprintln(e);Systemoutprintln("开始读取中1");

}

catch(IOException e)

{Systemoutprintln(e);Systemoutprintln("开始读取中2");}

}

public void writeInfoToFile(String fileName)

{

if(!flag) 如果标记前面的档案不存在,则return

{

flag=true; 改回原来的档案标记符

return;

}

try

{

newFile=new File(fileName);

if(newFileexists()) 如果存在,不用建立新档案

{

Systemoutprintln("档案存在,可以写入!");

}

else 如果不存在,则建立一个新档案

{

Systemoutprintln("档案不存在,准备建立新档案");

newFilecreateNewFile();

Systemoutprintln("档案建立成功,可以写入");

}

bw=new BufferedWriter(new FileWriter(newFile,true));

Systemoutprintln("totalString="+totalString);

bwwrite(totalString,0,totalStringlength());

bwflush(); 重新整理缓冲区

Systemoutprintln("写入完成");

totalString="\r\t"; 清空原来的字串

}

catch(FileNotFoundException e)

{Systemoutprintln(e);}

catch(IOException e)

{Systemoutprintln(e);}

}

}

________________第二个类______一个自定义的执行绪类____________________

package pro;

import javalangThread;

public class MyThread extends Thread

{

private int index; 用于阵列的位置

private String[] fileNames; 定义一个字串阵列

ReadFileToWriteOtherFile bfo=new ReadFileToWriteOtherFile(); 定义前面的自定义类

public MyThread(String[] fileNames,int index) index表示阵列位置标号

{

thisindex=index;

thisfileNames=fileNames;

}

public void run()

{

bforeadInfoFromFile(fileNames[index]);传入阵列中的字串引数

bfowriteInfoToFile("btxt"); 传入写入的目的地档案

index++; 阵列位置加1

Systemoutprintln("==============");分隔线

}

}

________________第三个类______主程式____________________

package pro;

import springframeworkcontextApplicationContext;

import springframeworkcontextsupportClassPathXmlApplicationContext;

import javaio;

public class BeanRunApp {

/

Method main

@param args

/

public static void main(String[] args)

{

/ ApplicationContext apc=new ClassPathXmlApplicationContext("beansxml");

ClassRoom croom=(ClassRoom)apcgetBean("classRoom");

croomout();

Systemoutprintln("over");

/

long startTime=SystemcurrentTimeMillis();

String[] a={"atxt","ctxt","dtxt","etxt"}; 用一个符品阵列储存档名

for(int i=0;i<alength;i++) 用阵列的长度来作为回圈条件

{ 把这个阵列和i的值作为建构函式传入执行绪类

MyThread myth=new MyThread(a,i);

Systemoutprintln("--------------------------------");

mythstart(); 执行

Systemoutprintln("当前的执行绪是:"+mythgetName());

}

long endTime=SystemcurrentTimeMillis();

Systemoutprintln("耗时:"+(endTime-startTime)+"毫秒");

}

}

C#中,怎样将资料写入到xml档案中

XmlTextWriter w = new XmlTextWriter(ConsoleOut);

wFormatting = FormattingIndented;

wWriteStartElement("x","root","urn:1");

wWriteStartElement("y","item","urn:1");

wWriteAttributeString("attr","urn:1","123");

wWriteEndElement();

wWriteEndElement();

wClose();

<x:root xmlns:x="urn:1">

<y:item y:attr="123" xmlns:y="urn:1" />

</x:root>

c# 将资料写入到excel档案中

sheet名Cells[3, 3] = "aaa";

将档案f1txt的内容读出来,然后写入到f2txt档案中 java实验报告

public static void main(String[] args) throws IOException { readFile("C:\\f1txt") ; writeFile("c:\\f2txt") ; } / 读档案 @param file @return / public static boolean readFile(String file) { try { FileReader fr = new FileReader(new File(file)); BufferedReader br = new BufferedReader(fr); String str = ""; while ((str = brreadLine()) != null) { listadd(str); } return true; } catch (FileNotFoundException e) { eprintStackTrace(); } catch (IOException e) { eprintStackTrace(); } return false; } / 写档案 @param filename @return / public static boolean writeFile(String filename) { try { FileWriter fw = new FileWriter(new File(filename)); BufferedWriter bw = new BufferedWriter(fw); for (String string : list) { bwwrite(string + "\r\n"); } bwflush(); bwclose(); return true; } catch (IOException e) { eprintStackTrace(); } return false; }

Java程式设计(基于图形介面)从档案中读出资料,显示在文字框中

读档案 ,使用javaioFileInputStream 可以,,,,,,如果有格式,可以按格式处理

~

~

~

~

labview从档案中读资料?

避免使用Case结构的隧道就可以了

C语言中如何将资料写到档案中?如何从档案中读出资料通过一个完整的程式举例说明

#include<stdioh>

int main()

{

char read[100];

FILE fpe;

if( (fpe= fopen("1txt", "r+"))==NULL )

{

puts("档案打开出错!");

return;

}

fputs("abcdefghijkflmnopqrstuvw", fpe);将abcdefghijkflmnopqrstuvw写到档案

rewind(fpe);移动档案位置指标到一个档案的开始处

fgets(read, 27, fpe);读取档案到read

fclose(fpe);关闭档案

printf("%s\n", read);输出

}

JAVA中如何将Vector从档案中读出

for回圈遍历

Vector v=new Vector();

for(int i=0;i<vsize();i++){

Systemoutprintln(vget(i));

}

设置sheet名称和单元格内容为中文

wbsetSheetName(n, "中文",HSSFCellENCODING_UTF_16);

cellsetEncoding((short) 1);

cellsetCellValue("中文");

public class Excel {

private jxlWorkbook rwb = null;

/

得到当前工作薄的总列数

@parma sheetIndex 工作薄号

@return int

/

public int getColCount(int sheetIndex) {

int colCnt = 0;

try {

jxlSheet rs = rwbgetSheet(sheetIndex);

colCnt = rsgetColumns();

} catch (Exception e) {

colCnt = 0;

} finally {

try {

} catch (Exception e) {

colCnt = 0;

}

}

return colCnt;

}

/

得到当前工作薄的总行数

@parma sheetIndex 工作薄号

@return int

/

public int getRowCount(int sheetIndex) {

int colCnt = 0;

try {

jxlSheet rs = rwbgetSheet(sheetIndex);

colCnt = rsgetRows();

} catch (Exception e) {

colCnt = 0;

} finally {

try {

} catch (Exception e) {

colCnt = 0;

}

}

return colCnt;

}

/

打开Excel

@parma fileName Excel文件名+文件路径(绝对路径)

@return boolean

/

public boolean openExcel(String fileName) {

boolean Rtn = false;

try {

is = new FileInputStream(fileName);

rwb = WorkbookgetWorkbook(is);

Rtn = true;

} catch (Exception e) {

Rtn = false;

} finally {

try {} catch (Exception e) {}

}

return Rtn;

}

/

取得某个单元格的内容。不论单元格是何种数据类型都将返回字符型。

@parma int col 列号 int row 行号

@return String

/

public String getCellContent(int col, int row) {

String cellContent = "";

try {

// 默认打开第一张工作薄。

Sheet rs = rwbgetSheet(0);

// 取得某一单元格的内容

Cell c00 = rsgetCell(col, row);

cellContent = c00getContents();

} catch (Exception e) {

cellContent = "";

} finally {

try {

} catch (Exception e) {

cellContent = "";

}

}

return cellContent;

}

public static void main(String[] args) {

Excel ex = new Excel();

exopenExcel("你自己的xls");

for (int i = 1; i < exgetRowCount(0); i++) {

for (int j = 0; j < exgetColCount(0); j++) {

Systemoutprintln(exgetCellContent(j, i));

}

}

}

}

easyExcel要读取所有的sheet表格。但是要除去一个叫测试的sheetName的sheet

在谷歌上找到答案,可以通过以下方式获得所有的sheetName名字。

>

第一步:如何用POI *** 作Excel

@Test

public void createXls() throws Exception{

//声明一个工作薄

HSSFWorkbook wb = new HSSFWorkbook();

//声明表

HSSFSheet sheet = wbcreateSheet("第一个表");

//声明行

HSSFRow row = sheetcreateRow(7);

//声明列

HSSFCell cel = rowcreateCell(3);

//写入数据

celsetCellValue("你也好");

FileOutputStream fileOut = new FileOutputStream("d:/a/bxls");

wbwrite(fileOut);

fileOutclose();

}

第二步:导出指定数据库的所有表

分析:

1:某个数数据库有多少表,表名是什么?―――DataBaseMetadategetMetadate()getTables(null,null,null,new String[]{Table}); - excel的文件名称。

2:对每一个表进行select *** 作。 - 每一个sheet的名称。

3:分析表结构,rsgetMetadate(); ResultSetMedated

4:多个列,列名是什么- 字段名就是sheet的第一行信息。

5:获取每一行的数据 – 放到sheet第一行以后。

@Test

public void export() throws Exception{

//声明需要导出的数据库

String dbName = "focus";

//声明book

HSSFWorkbook book = new HSSFWorkbook();

//获取Connection,获取db的元数据

Connection con = DataSourceUtilsgetConn();

//声明statemen

Statement st = concreateStatement();

//stexecute("use "+dbName);

DatabaseMetaData dmd = congetMetaData();

//获取数据库有多少表

ResultSet rs = dmdgetTables(dbName,dbName,null,new String[]{"TABLE"});

//获取所有表名 - 就是一个sheet

List<String> tables = new ArrayList<String>();

while(rsnext()){

String tableName = rsgetString("TABLE_NAME");

tablesadd(tableName);

}

for(String tableName:tables){

HSSFSheet sheet = bookcreateSheet(tableName);

//声明sql

String sql = "select from "+dbName+""+tableName;

//查询数据

rs = stexecuteQuery(sql);

//根据查询的结果,分析结果集的元数据

ResultSetMetaData rsmd = rsgetMetaData();

//获取这个查询有多少行

int cols = rsmdgetColumnCount();

//获取所有列名

//创建第一行

HSSFRow row = sheetcreateRow(0);

for(int i=0;i<cols;i++){

String colName = rsmdgetColumnName(i+1);

//创建一个新的列

HSSFCell cell = rowcreateCell(i);

//写入列名

cellsetCellValue(colName);

}

//遍历数据

int index = 1;

while(rsnext()){

row = sheetcreateRow(index++);

//声明列

for(int i=0;i<cols;i++){

String val = rsgetString(i+1);

//声明列

HSSFCell cel = rowcreateCell(i);

//放数据

celsetCellValue(val);

}

}

}

conclose();

bookwrite(new FileOutputStream("d:/a/"+dbName+"xls"));

}

使用jxl

public static void main(String[] args) throws Exception {

Workbook wb = WorkbookgetWorkbook(new FileInputStream("xxxxls"));

Sheet[] sheets = wbgetSheets();

Sheet sheet = sheets[0];

String[] arr1 = new String[sheetgetRows()];

String[] arr2 = new String[sheetgetRows()];

for (int i = 0; i < sheetgetRows(); i++) {

Cell[] cells = sheetgetRow(i);

arr1[i] = cells[0]getContents()trim();

arr2[i] = cells[1]getContents()trim();

}

Systemoutprintln(ArraystoString(arr1));

Systemoutprintln(ArraystoString(arr2));

}

好几年前的提问了,可能还有人会有相同的疑问。我刚好也找到了方法。其实不难

//建立一个workbook 实例

HSSFWorkbook wb = new HSSFWorkbook(poifs);

//通过workbook的getNumberOfSheets方法返回sheet总数就可以了

wbgetNumberOfSheets();

以上就是关于java档案 *** 作怎样从档案中读出资料然后写入到另外一个档案中全部的内容,包括:java档案 *** 作怎样从档案中读出资料然后写入到另外一个档案中、Java的poi技术 重命名Sheet、如何用JAVA把EXCEL表格读出来(不用数据库)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9647991.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存