文档库 最新最全的文档下载
当前位置:文档库 › C#创建及追加txt文件

C#创建及追加txt文件

C#创建及追加txt文件

PathTxb.Text = Application.StartupPath;

filename = PathTxb.Text + "/测试报告.txt";

if (!File.Exists(filename))

{

FileStream fs1 = new FileStream(filename, FileMode.Create, FileAccess.Write);//创建写入文件

StreamWriter sw = new StreamWriter(fs1);

sw.WriteLine("蒋水兵");//开始写入值

sw.Close();

fs1.Close();

}

else

{

//FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Write);

StreamWriter sr = File.AppendText(filename); sr.WriteLine("追加文件");//开始写入值

sr.Close();

// fs.Close();

}

java创建txt文件并进行读写修改操作

java创建txt文件并进行读写修改操作 import java.io.*;/** * * 功能描述:创建TXT文件并进行读、写、修改操作* */ public class ReadWriteFile { public static BufferedReader bufread; //指定文件路径和名称 private static String path = "D:/suncity.txt"; private static File filename = new File(path); private static String readStr =""; /** * 创建文本文件. * @throws IOException * */ public static void creatTxtFile() throws IOException{ if (!filename.exists()) { filename.createNewFile(); System.err.println(filename + "已创建!"); }

} /** * 读取文本文件. * */ public static String readTxtFile(){ String read; FileReader fileread; try { fileread = new FileReader(filename); bufread = new BufferedReader(fileread); try { while ((read = bufread.readLine()) != null) { readStr = readStr + read+ "\r\n"; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block

java文件流IO的增删改基础

package cn.itcast.file; import java.io.File; /* IO流(Input Output) : IO技术主要的作用是解决设备与设备之间的数据传输问题。比如:硬盘--->内存内存的数据---->硬盘上把键盘的数据------->内存中 IO技术的应用场景: 导出报表,上传大头照、下载、解释xml文件 ... 数据保存到硬盘上,该数据就可以做到永久性的保存。数据一般是以文件的形式保存到硬盘上 sun使用了一个File类描述了文件或者文件夹的。 File类可以描述一个文件或者一个文件夹。 File类的构造方法: File(String pathname) 指定文件或者文件夹的路径创建一个File文件。 File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。

File(String parent, String child) 目录分隔符:在windows机器上的目录分隔符是 \ ,在linux机器上的目录分隔符是/ . 注意:在windows上面\ 与 / 都可以使用作为目录分隔符。而且,如果写/ 的时候只需要写一个即可。 */ public class Demo1 { public static void main(String[] args) { //File file = new File("F:"+File.separator+"a.txt"); // 在linux机器上是不是一个合法路径??? File file = new File("F:/a.txt"); /*File parentFile = new File("F:\\"); File file = new File("F:\\","a.txt");*/ System.out.println("存在吗? "+ file.exists()); // exists 判断该文件是否存在,存在返回true,否则返回false。 // System.out.println("目录分隔符:"+ File.separator); } } package cn.itcast.file; import java.io.File;

java生成word文档

package com.sample; import java.awt.Color; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.rtf.RtfWriter2; /** * * @author wangyanjun * @email bd_wyj@https://www.wendangku.net/doc/9712968340.html, * @createDate Jun 12, 2008 */ public class CreateWordDemo { public void createDocContext(String file) throws DocumentException, IOException { // 设置纸张大小 Document document = new Document(PageSize.A4); // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 标题字体风格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); // 正文字体风格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); Paragraph title = new Paragraph("标题");

JAVA生成XML文件

package com.inspur.test; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class FileDemo { /** *@param args */ public static void main(String[] args) { // TODO Auto-generated method stub String filename="c:\\text.xml"; File file=new File(filename); /** *判断文件是否存在.如果文件不存在则生成一个文件 *则生成一个文件;反之则先删除原有文件,生成一个新的文件 */ if(file.exists()){ try { file.delete(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //创建根节点 Element root=new Element("ObjFlow"); //根节点添加到文档中 Document document=new Document(root); //添加工单基本信息 //创建工单基本信息的节点 Element baseinfo=new Element("BaseInfo"); //创建工单编号节点 Element formOid=new Element("formOid"); //添加工单编号节点内容 formOid.setText("工单编号"); //将工单编号节点添加到基本信息节点下

java创建文件夹

java创建文件夹 主要是程序红色部分 用java建立文件夹 package test; public class muluTest { public static void main(String[] args) { muluTest mulutest = new muluTest(); mulutest.newFolder("D:\\abcds"); } public void newFolder(String folderPath) { String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); try { if(myFilePath.isDirectory()) { System.out.println("the directory is exists!"); } else { myFilePath.mkdir(); System.out.println("新建目录成功"); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace(); } } }

java创建文件夹,文件;删除文件夹,文件 package ifpre.web; import gxdmif.GXStringUtil; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import org.springframework.web.multipart.MultipartFile; public class SaveFile { public boolean save(String path, MultipartFile file) throws Exception { GXStringUtil gx = new GXStringUtil(); boolean result = false ; File dirFile = null ; try { dirFile = new File(path); if ( ! (dirFile.exists()) && ! (dirFile.isDirectory())) { boolean creadok = dirFile.mkdirs(); if (creadok) { System.out.println( " ok:创建文件夹成功!" ); } else { System.out.println( " err:创建文件夹失败!" ); } } } catch (Exception e) { e.printStackTrace(); System.out.println(e); return false ; } if (file != null && ! file.isEmpty()) { String fullPath = path + System.getProperty( " file.separator " ) + https://www.wendangku.net/doc/9712968340.html,StringToGBK(file.getOriginalFilename()); DataOutputStream out = null ; InputStream is = null ; try { out = new DataOutputStream( new FileOutputStream(fullPath)); is = file.getInputStream(); byte [] buffer = new byte [ 1024 ]; while (is.read(buffer) > 0 ) { out.write(buffer);

java生成pdf文件

public static void writeSimplePdf() throws Exception{ //1.新建document对象 //第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。 Document document = new Document(PageSize.A4, 50, 50, 50, 50); //2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 //创建PdfWriter 对象第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf")); //3.打开文档 document.open(); //4.向文档中添加内容 //通过com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落 document.add(new Paragraph("First page of the document.")); document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200)))); //5.关闭文档 document.close(); } /** * 添加含有章节的pdf文件 * @throws Exception */ public static void writeCharpter() throws Exception{ //新建document对象第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。 Document document = new Document(PageSize.A4, 20, 20, 20, 20); //建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。 PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("c:\\ITextTest.pdf")); //打开文件 document.open(); //标题 document.addTitle("Hello mingri example"); //作者 document.addAuthor("wolf"); //主题 document.addSubject("This example explains how to add metadata."); document.addKeywords("iText, Hello mingri");

java新建文件并移动文件

package com.djwl.test.huxiao; import java.io.File; import com.djwl.core.BaseTestCase; public class FileMove extends BaseTestCase { /** * 功能描述:创建path目录及其父目录
* * @param path * @author 胡晓
* 时间:2009-10-22 下午05:01:33
*/ public void createFolder(String path) { try { File file = new File(path); if (!file.exists()) { file.mkdirs(); } } catch (Exception e) { System.out.println("创建文件夹出错"); e.printStackTrace(); } } /** * 功能描述:把filename从from文件夹移动到to文件夹
* * @param from * @param to * @author 胡晓
* 时间:2009-10-22 下午05:08:21
*/ public void removeFromTo(String from, String to, String filename) { createFolder(to); File file = new File(from, filename); if (file.isFile()) { System.out.println("移动" + filename); file.renameTo(new File(to, file.getName())); } else { System.out.println(filename + "不存在"); }

Java实现简单文件下载

java实现简单的文件下载 文件下载需要在WebRoot 下创建images文件夹并且放入test.txt文件创建jsp文件夹放入01.jsp index.jsp 不变web.xml需要配置servlet 适合初学者研究学习 //index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'index.jsp' starting page [下载] //jsp文件夹下的01.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> 下载:test1.txt ${errorResult} //文件下载serlvet package com.kero99.ygc.download; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet {

JAVA生成word文档代码加说明

import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Iterator; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.model.FieldsDocumentPart; import https://www.wendangku.net/doc/9712968340.html,ermodel.Field; import https://www.wendangku.net/doc/9712968340.html,ermodel.Fields; import https://www.wendangku.net/doc/9712968340.html,ermodel.Range; import https://www.wendangku.net/doc/9712968340.html,ermodel.Table; import https://www.wendangku.net/doc/9712968340.html,ermodel.TableIterator; import https://www.wendangku.net/doc/9712968340.html,ermodel.TableRow; publicclass WordUtil { publicstaticvoid readwriteWord(String filePath, String downPath, Map map, int[] num, String downFileName) { //读取word模板 FileInputStream in = null; try { in = new FileInputStream(new File(filePath)); } catch (FileNotFoundException e1) { e1.printStackTrace(); } HWPFDocument hdt = null; try { hdt = new HWPFDocument(in); } catch (IOException e1) { e1.printStackTrace(); } Fields fields = hdt.getFields(); Iterator it = fields.getFields(FieldsDocumentPart.MAIN) .iterator(); while (it.hasNext()) { System.out.println(it.next().getType()); }

java千万级别数据生成文件思路和优化

java千万级别数据生成文件思路和优化 现场提的问题概况: 数据量:生成xml,每个文件100W+ 条的数据 内存控制:最好不要超过512M 问题详情:在处理70W左右的时候内存溢出 一、先来看一下程序要生成的xml文件的结构 Xml代码 1. 2.1 3.12 4.03 5.004 6.5 7.0006 8.1000000 9. 10. 11. 10350719507 12. 1 13. 20110303 14. 20110419 15. 45000 16. 17. ... 18. 二、给大家说一下如何把大数据生成xml文件

1、小数据量的情况下 < 1W条数据 比较好用的方法是使用开源框架,比如XStream 直接把javabean 生成 xml 优点:api操作简单,方便维护 缺点:数据量大的情况下太消耗内存 2、大数据量生成一个xml文件(本程序采用的方法) 自己做的一个可以使用极少的内存生成无限制大的xml文件框架由3部分生成xml文件 第一部分:生成文件头 例如: xxx.toXML(Object obj, String fileName) 第二部分:通过每次向文件里面追加3000(可配置)条数据的形式生成文件块 例如:xxx.appendXML(Object object); //object 可以是ArrayList 或者一个单独的javaBean 第三部分:生成xml文件尾巴 例如:xxx.finishXML();

java生成pdf模板

竭诚为您提供优质文档/双击可除java生成pdf模板 篇一:java生成pdf 如今pdF格 式文档的使用已 经越来越普遍, 它在文档压缩、 安全等方面都表 现的非常优秀, 有关它的好处我 就不在这里多说 了。那么如何使 用java语言开 发应用来输出 pdF格式的文档 呢?在这里给大 家介绍itext, 它是一个开发源 代码的项目,你

可以使用itext 方便的实现pdF 的输出。这篇文章我将向大家介绍如何使用 itext来生成pdF文档。helloworld 作为一个程 序开发人员,对于helloworld 程序并不陌生,几乎每一种语言或应用开始总是会举一个helloworld的例子来向大家介绍。在开始介绍itext时,我们不妨也从helloworld开始

现在运行上 面的代码(记住 在这之前把 itext.jar放到 你的classpath 之中),如果一 切正常的话你会 在”c:/”看到 一个名为 hello.pdf的文 件。打开这个文 件,看到了什 么?是的文档里 有一行字符 “helloworld”, 如下图。 怎么样是不 是很简单?当然 我们在实际应用时不可能只是简单的输出一个字 符串就完事了, 还要作很多的工

作,输出更复杂 的pdF,下面就 让我们开始进一 步了解itext的 其他功能。 更复杂的设置 分析一下 document的构 造方法,我们发 现除了我们上一 个例子中的无参 数构造以外还有 两个: /* *createdon20xx-1-3,创建第一个helloworld程序*/ packagetest1; importjava.io.FilenotFoundexception; importjava.io.Fileoutputstream; importcom.lowagie.text.*; importcom.lowagie.text.pdf.*; publicclasshelloworld{

javaFile文件操作和文件流的详解(福哥出品)

一. 创建文件 (1)最常用的(获得一个固定路径下的文件对象) File parentFile = new File“( D:\\My Documents\\ ”);// 参数 是一个路径的字符串。 (2)在父目录创建一个名为child 的文件对象, child 为文件对象的名字 File chileFile= new File(“ D:\\My Documents\\ ................................................. ” ,Stri ng child); 或 File chileFile= new File(parentFile,String child); 二,常见文件夹属性和方法 (1)createNewFile();该方法的作用是创建指定的文件。该方法只能用于创建文件,不能用于创建文 件夹,且文件路径中包含的文件夹必须存在 File file=new ("D:\\My Document\\text.txt"); file.createNewFile(); 这样就会在 D 盘下的 My Document 创建 text.txt 的记事本(注意:首先得保 证 D 盘下有 My Documen 这个文件夹) (2)mkdir();

根据File对象的名字(路径)创建一个目录(文件夹),如果是相对目录,则新建的目 录在当前目录下 (3)mkdirs(); 如果 File 对象名字有多级目录,则可以调用该方法一次性创建多级目录。 (4)exists(); 判断 File 对象指向的文件是否存在,返回一个 boolean 类型 (5)isDirectory(); 判断 File 对象指向的文件是否为目录,返回一个 boolean 类型的值, true 或者 false。 (6) getName();获得文件名称(不带路径) ( 7) length (); 得到 File 对象指向文件的长度,以字节计算,返回一个长整 形的值(Io ng);注意:在 系统中,文件夹(目录)的大小为零,也就是不占用空间,使用length()时返回的是0 ( 8) deIete() ; 删除 File 对象所指定的文件 ( 9) isFile(); 判断 File 对象指向的文件是不是标准文件(就像图片,音乐文件等)

java+根据word模板生成word+文档

java 根据word模板生成word 文档 先下载jacob_1.10.1.zip。 解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。 将jacob.jar加入项目。 /* * Java2word.java * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /* * 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。* word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……,以免执行错误的替换。 * 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、 .jpg、.gif)。 * 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表 word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代 表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、 第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。 */ package com.word.util; /** * * @author kdl */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import com.jacob.activeX.ActiveXComponent; import https://www.wendangku.net/doc/9712968340.html,.Dispatch; import https://www.wendangku.net/doc/9712968340.html,.Variant; public class Java2word { private boolean saveOnExit; /**

java 生成word文件并下载

importjava.io.BufferedInputStream; importjava.io.BufferedOutputStream; importjava.io.BufferedWriter; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.InputStream; importjava.io.OutputStream; importjava.io.OutputStreamWriter; importjava.io.Writer; https://www.wendangku.net/doc/9712968340.html,.URLEncoder; importjava.util.Map; importjavax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; importfreemarker.template.Configuration; importfreemarker.template.Template; /** * @Desc:word操作工具类 */ public class WordUtil { private static Logger log = Logger.getLogger(WordUtil.class); /** * @Desc:生成word文件 * @paramdataMap word中需要展示的动态数据,用map集合来保存 * @paramtemplateName word模板名称,例如:test.ftl * @paramfilePath文件生成的目标路径,例如:D:/wordFile/ * @paramfileName生成的文件名称,例如:test.doc */ public static void createWord(MapdataMap,StringtemplateName,StringfilePath,StringfileName){ try { //创建配置实例 Configuration configuration = new Configuration(); //设置编码 configuration.setDefaultEncoding("UTF-8"); //ftl模板文件 File file = new File(filePath); configuration.setDirectoryForTemplateLoading(file);

java创建TXT文件并进行读、写、修改操作

java创建TXT文件并进行读、写、修改操作 文章分类:Java编程关键字: java创建txt文件并进行读、写、修改操作import java.io.*; /** *//** * * 功能描述:创建TXT文件并进行读、写、修改操作 * * @author KenZhang * @version 1.0 * Creation date: 2007-12-18 - 下午06:48:45 */ public class ReadWriteFile { public static BufferedReader bufread; //指定文件路径和名称 private static String path = "D:/suncity.txt"; private static File filename = new File(path); private static String readStr =""; /** *//** * 创建文本文件. * @throws IOException * */ public static void creatTxtFile() throws IOException{ if (!filename.exists()) { filename.createNewFile(); System.err.println(filename + "已创建!"); } } /** *//** * 读取文本文件. * */ public static String readTxtFile(){ String read; FileReader fileread; try { fileread = new FileReader(filename); bufread = new BufferedReader(fileread);

java生成word文件并下载

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import https://www.wendangku.net/doc/9712968340.html,.URLEncoder; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import freemarker.template.Configuration; import freemarker.template.Template; /** * @Desc:word 操作工具类 */ public class WordUtil { private static Logger log = Logger.getLogger(WordUtil.class); /** * @Desc :生成word 文件 * @param dataMap word 中需要展示的动态数据,用map 集合来保存 * @param templateName word 模板名称,例如:test.ftl * @param filePath 文件生成的目标路径,例如:D:/wordFile/ * @param fileName 生成的文件名称,例如:test.doc */ public static void createWord(Map dataMap,String templateName,String filePath,String fileName){ try { // 创建配置实例 Configuration configuration = new Configuration(); // 设置编码 configuration.setDefaultEncoding("UTF-8"); //ftl 模板文件 File file = new File(filePath); configuration.setDirectoryForTemplateLoading(file); // 获取模板 Template template = configuration.getTemplate(templateName); // 输出文件

如何用记事本编写一个简单的java代码

. .. 一:怎么用记事本写第一个java代码? 第一步:新建txt文档 首先我们在F盘新建一个txt文档,并命名为Test.java,如下图所示: 第二步:编写代码 我们双击打开:如下图所示 第三步:编写好代码 我们在记事本中写代码,写好后如下所示:. z.

. .. 然后我们同时按Ctrl+s键保存,然后关闭记事本即可 上面的源代码如下: 第三步:打开控制台界面 我们在计算机中,打开如下图界面, . z. . ..

然后在这里,输入cmd,并按回车,然后弹出如图所示 . z. . ..

第四步:在控制台中运行并输出 我们用鼠标点击一下里面黑色部分,然后输入F:然后回车,然后再输入javac Test.java然后回车,最后输入java Test,就可以看到结果了,如下图所示: . z. . .. 红色圈出来部分就是结果了 二:温馨提示: 1,上面演算中在控制台中首先输入F:的原因是 因为我在F盘建立一个文本文档,并命名为Test.java,如果你是把你的文档文档建立在D盘,则应该输入D: 以此类推。

2,如果你是在F盘下的CH文件夹建立一个记事本,并命名为Test.java,则输入的情况如下, cd CH表示转到CH这个文件夹下,然后再输入java.Test.java回车,再输入java Test就可以看到运行结果了 3,如果你在D盘下的CH的文件夹下新建一个为ch的文件夹,并且在ch的文件夹下新建一个txt 文. z. . .. 档,并命名为Main.java,则书写的代码如下:public class Main{//这里Main必须和Main.java中的Main一致 public static void main(String[] args){

相关文档