java写入txt文件

用java生成txt文件有有两种方式,一种是通过字符流(或字节流),另一种是直接调用PrintWriter类。

1、字符流(字节流)

代码如下:

import java.io.File;
import java.io.FileOutputStream;

public class TxtWirte {
 public void DOWriteTxt(String file, String txt) {
  try {
   FileOutputStream os = new FileOutputStream(new File(file), true);
   os.write((txt + "\n").getBytes());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  new TxtWirte().DOWriteTxt("D:\\问好.txt", "你好!");
 }
}

2、调用PrintWriter类

import java.io.*;

public class C {
    public static void main( String[ ] args ) throws Exception {
        PrintWriter pw = new PrintWriter( new FileWriter( "D;\\问好.txt" ) );
        pw.print("你好" );
        pw.close();
    }
}

  如果要生成doc文件,将“D:\\问好.txt”改成“D:\\问好.doc”即可。

 

 

 

 


的:java写一定内容到指定路径的文件中

程序源码,1.50版本执行成功:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class WriteToDoc {

 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  byte[] buffer=new byte[1024];
  boolean cont=true;  //循环控制变量
  FileOutputStream outfile=null;  //文档输出对象
  //生成对象outfile,准备输出到文档
  try
  {
   outfile=new FileOutputStream(args[0]);
  }catch(FileNotFoundException e)
  {
   System.out.println("File Not Found!");
   System.exit(1);
  }
  //行首没有输入句号时执行以下循环
  while(cont)
  {
   try{
    //从控制台读入数据
    int n=System.in.read(buffer);
    //输出到控制台
    System.out.println(n);
    if(buffer[0]=='#')
    {
     cont=false;
    }
    else
    {
     outfile.write(buffer, 0, n);
    }
   }catch(Exception e)
   {
    System.exit(1);
   }
  }
  try{
   outfile.close();
  }catch(IOException e)
  {
   System.err.println("文档错误");
   System.exit(1);
  }
 }

}
分析:
FileOutputStream的write方法原型:
 public void write(byte[] b,
                   int off,
                   int len)
             throws IOException
第一个参数:表示从哪里读取数据
第二个参数:表示数据从什么地方开始写,比如如果数据为"hahatest",而off=4的话,则写到文本里边的是test
第三个参数:写入数据的长度,本例中n=System.in.read得到的,假如输入的数据为"haha",则n=6,因为加了一个换行进去,如果想把输入不同行的数据最后写到一行里边,需要n-2。

java写中文,如果是有长度限制,使用outFile.write(name.getBytes(),0,name.getBytes().length);这样即使name是中英文混和的也能处理
import java.io.*;
public class javaCh
{
 public static void main(String[] arg)
 {
  FileOutputStream outFile=null;
  try{
   outFile=new FileOutputStream("test.txt");
   String name="李小毛";
   outFile.write(name.getBytes());
   }catch(IOException e)
   {
    System.exit(1);
    }
  finally
  {
   try{
    outFile.close();
     }catch(IOException e)
     {
      System.exit(1);
      }
   }
  }
 }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郡麟天下

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值