javascript怎么把一个字符串输出成文件

JavaScript010

javascript怎么把一个字符串输出成文件,第1张

在action里指定response对象参数即可,不需要再服务器生成文件,直接字符串写入流

response.setContentType("application/x-msdownload")

response.setHeader("Content-Disposition",

"attachment" + " filename="+pid+".html")

response.getOutputStream().write(text.getBytes())

response.getOutputStream().flush()

}

新建1.html把以下内容保存在 1.html里面

<!-- saved from url=(0014)about:internet -->

<script type="text/javascript">

function file(){

var fso= new ActiveXObject("Scripting.FileSystemObject")

if(!fso.FileExists("C:/Documents and Settings/Administrator/桌面/1.txt")){

var f1 = fso.createtextfile("C:/Documents and Settings/Administrator/桌面/1.txt",true)

f1.write("这是您创建的一个文本文档")

var openf1 = fso.OpenTextFile("1.txt")

// 读取文件一行内容到字符串

str = openf1.ReadLine()

alert("里面的内容为'" + str + "'")

}

}

</script>

<button onclick='file()'>在桌面上创建一个文本文件</button>

1、首先用java生成txt文件有有两种方式,一种是通过字符流(或字节流),另一种是直接调用PrintWriter类。字符流(字节流)代码如下:import java.io.Fileimport java.io。

2、FileOutputStreampublic class TxtWirte { public void DOWriteTxt(String file, String txt) {  try {  FileOutputStream os = new FileOutputStream(new File(file), true)  。

3、os.write((txt + "\n").getBytes())  } catch (Exception e) {   e.printStackTrace()  } } public static void main(String[] args) {  new TxtWirte().DOWriteTxt("D:\\问好.txt", "你好!")}}。

4、调用PrintWriter类:import java.io.*public class C {   public static void main( String[ ] args ) throws Exception {  PrintWriter pw = new PrintWriter( new FileWriter( "D\\问好.txt" ) )。

5、pw.print("你好" )pw.close()}}如果要生成doc文件,将“D:\\问好.txt”改成“D:\\问好.doc”即可。的:java写一定内容到指定路径的文件中程序源码。

6、最后循环控制变  FileOutputStream outfile=null  //文档输出对  //生成对象outfile。就完成了。