JAVA 的输入输出,读取写入文件
//将内容追加到文件尾部import java.io.FileWriter;import java.io.IOException;import java.io.RandomAccessFile;/*** 将内容追加到文件尾部*/public class AppendToFile {/** * A方法追加文件:使用RandomAccessFile * @param fileName 文件名 * @param content 追加的内容 */public static void appendMethodA(String fileName, String content){ try { // 打开一个随机访问文件流,按读写方式 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw"); // 文件长度,字节数 long fileLength = randomFile.length(); //将写文件指针移到文件尾。
randomFile.seek(fileLength); randomFile.writeBytes(content); randomFile.close(); } catch (IOException e){ e.printStackTrace(); }}/** * B方法追加文件:使用FileWriter * @param fileName * @param content */public static void appendMethodB(String fileName, String content){ try { //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content); writer.close(); } catch (IOException e) { e.printStackTrace(); }}public static void main(String[] args) { String fileName = "C:/temp/newTemp.txt"; String content = "new append!"; //按方法A追加文件 AppendToFile.appendMethodA(fileName, content); AppendToFile.appendMethodA(fileName, "append end. "); //显示文件内容 ReadFromFile.readFileByLines(fileName); //按方法B追加文件 AppendToFile.appendMethodB(fileName, content); AppendToFile.appendMethodB(fileName, "append end. "); //显示文件内容 ReadFromFile.readFileByLines(fileName);}}本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2010/0323/21269_2.php
JAVA中如何将生成的数据写入到文件
这个用到java io的知识:Java.io包几乎包含了所有操作输入、输出需要的类。
所有这些流类代表了输入源和输出目标。
Java.io包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。
一个流可以理解为一个数据的序列。
输入流表示从一个源读取数据,输出流表示向一个目标写数据。
Java为I/O提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。
但本节讲述最基本的和流与I/O相关的功能。
我们将通过一个个例子来学习这些功能。
输入:Java的控制台输入由System.in完成。
为了获得一个绑定到控制台的字符流,你可以把System.in包装在一个BufferedReader 对象中来创建一个字符流。
下面是创建BufferedReader的基本语法:BufferedReader br = new BufferedReader(new InputStreamReader(System.in));BufferedReader对象创建后,我们便可以使用read()方法从控制台读取一个字符,或者用readLine()方法读取一个字符串。
实例:// 使用 BufferedReader 在控制台读取字符import java.io.*;public class BRRead {public static void main(String args[]) throws IOException{char c;// 使用 System.in 创建 BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("Enter characters, 'q' to quit.");// 读取字符do {c = (char) br.read();System.out.println(c);} while(c != 'q');}}
如何通过java输出特殊符号到excel/word文档中
import java.io.*;展开全部class Main{ public static void main(String[] args) throws Exception{ FileWriter fw=new FileWriter("data.txt",true);//表示追加 fw.write("大家好!hello!bye"); fw.flush(); fw.close(); }}如上即可,简洁易懂。
。
。
...
怎么用java导出word文档
5、Word等文件、插入图片、插入表格等。
填充数据到表格中读取表格数据 ,1.1版增强的功能: 指定文本样式,指定表格样式,不能设置样式。
3:Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。
该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。
这些服务包括: 打开文档、新建文档。
Word从2003开始支持XML格式,大致的思路是先用office2003或者2007编辑好word的样式。
经测试这样方式生成的word文档完全符合office标准,样式。
如此,则可动态排版word文档、插入表格,在书签处插入文字,目前只能实现一些简单文件的操作,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc、查找文字、替换文字。
他的excel处理很强大,对于word还局限于读取、Html文件转化为PDF文件。
功能强大,但是处理样式有点缺陷,简单的导出可以使用。
6:用XML做就很简单了java导出word大致有6种解决方案:1:Jacob是Java-COM Bridge的缩写。
是一种不错的解决方案。
4:iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML。
使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。
DLL动态链接库的生成需要windows平台的支持。
该方案只能在windows平台实现,是其局限性。
2:Apache POI包括一系列的API,它们可以操作基于MicroSoft OLE 2 Compound Document Format的各种格式文件,可以通过这些API在Java中读写Excel,该方案实现简单,它在Java与微软的COM组件之间构建一座桥梁,插入文字、插入图片:JSP输出样式...
用java写导入导出txt文档
public class IOTest { public static void main (String args[]) { ReadDate(); WriteDate(); } /** * 读取数据 */ public static void ReadDate() { String url = "e:/2.txt"; try { FileReader read = new FileReader(new File(url)); StringBuffer sb = new StringBuffer(); char ch[] = new char[1024]; int d = read.read(ch); while(d!=-1){ String str = new String(ch,0,d); sb.append(str); d = read.read(ch); } System.out.print(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 写入数据 */ public static void WriteDate() { try{ File file = new File("D:/abc.txt"); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter(new FileWriter(file)); ArrayList ResolveList = new ArrayList(); for (int i = 0; i < 10; i++) { ResolveList.add(Math.random()* 100); } for (int i=0 ;i<ResolveList.size(); i++) { output.write(String.valueOf(ResolveList.get(i)) + "\n"); } output.close(); } catch (Exception ex) { System.out.println(ex); } } }
java导出word怎么做,最好有代码
展开全部 修改LogWriter类的静态域即可随意切换输出了。
main方法中代码不用改变。
代码如下:import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class WriteOut { public static void main(String[] args) throws IOException { LogWriter.log("log info..."); }}class LogWriter { // 可以写作配置:true写文件; false输出控制台 private static boolean fileLog = true; private static String logFileName = "/tmp/log.log"; public static void log(String info) throws IOException { OutputStream out = getOutputStream(); out.write(info.getBytes("utf-8")); } public static OutputStream getOutputStream() throws IOException { if (fileLog) { File file = new File(logFileName); if (!file.exists()) file.createNewFile(); return new FileOutputStream(file); } else { return System.out; } }}...
转载请注明出处51数据库 » java导出 写入word文档