❶ 如何将java程序输出的结果存到指定文件
Filefile=newFile("D:/test.txt");FileOutputStreamfos=newFileOutputStream(file);Stringcontent="这里是要输入的数据";fos.write(content.getBytes());fos.flush();fos.close();
❷ 如何用java输出txt文件
输入无需使用字节流,直接字符流读取即可。
privatevoidinput(StringfileName)throwsIOException{try(BufferedReaderreader=newBufferedReader(newFileReader(fileName))){Stringline;while((line=reader.readLine())!=null){System.out.println(line);}}}
同样输出,只要把Input换成Output;
privatevoidoutput(StringfileName,Stringcontent)throwsIOException{try(BufferedWriterwriter=newBufferedWriter(newFileWriter(fileName))){writer.write(content);writer.flush();}}
❸ 如何将java控制台的输出内容存入到文本文件中
修改LogWriter类的静态域即可随意切换输出了。方法中代码不用改变。代码如下: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;}}}
❹ java中怎样提取文本框中的内容输出到文件
作为表单提交,然后通过request.getParameter("元素name");就可以获得这些数据了。
❺ java中如何将输出结果放入文件中
这个就需要java中的I/O流来对文件进行读写,举个例子:以类来写文件
importjava.io.FileNotFoundException;importjava.io.FileWriter;importjava.io.IOException;publicclassTest{publicstaticvoidrwFile(){FileWriterfw=null;try{fw=newFileWriter("f:\text.txt",true);fw.write("123");//这里向文件中输入结果123fw.flush();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{if(fw!=null){try{fw.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}publicstaticvoidmain(String[]args){rwFile();}}
这个代码是向文件F盘的text.txt中输入123
❻ java如何把输出写到文件中
Java使用FileWriter实现文件的写入,用法为:FileWriter(file,true); 其中第二个参数设置成false就是覆盖写入,回true就是增量答存储。举例代码:import java.io.File;import java.io.FileWriter;import java.io.IOException;public class File01Demo { public static void main(String[] args) throws IOException { File file = new File("D:\\a.txt"); FileWriter fw = new FileWriter(file,true); //设置成true就是追加 fw.write("asd"); fw.write("\r\n"); fw.write("ffd"); fw.close(); }}
❼ 用JAVA怎样才能将数据输出到文件的指定位置
参考这程序吧/* * c7:IODemo4.java 1.0 05/02/10 * author:Leo * This program is protected by right laws. * Reader from and write to files. * 字符流和字节流类似,但效率更高.有可能的情况下,尽量使用reader和writer * 一般只要把Reader替换为,用Writer 替换OutputStream,当然还要用Char替换Byte. * 用reader和writer可以一次读一行,而且没有类型转换的麻烦 . */package myprojects.readerwriter;import java.io.*;class IODemo4{ public static void main(String args[]) { System.out.println("Starting reader…"); String s = new String(); String s2=new String(); try { BufferedReader in = new BufferedReader(new FileReader("d:\\classtry\\My.class")); PrintWriter out = new PrintWriter(new FileWriter("d:\\classtry\\destnation.xls")); //BufferedReader in2 = new BufferedReader(new FileReader("d:\\javatry\\classtry\\destnation.xls"));//把下面的字符串写入到文件的第一行.//out.println("the following is from source"); //下面的循环语句读入一行内容的同时进行写入. while((s=in.readLine()) != null) { System.out.println(s); //char ss[]=s.toCharArray(); out.println(s+"\t"); } //in2.close(); in.close(); out.close();//关闭文件. } catch(Exception e) { e.printStackTrace(); } }}
❽ 怎么将一个java程序的结果输出到文本文档中,写一段代码,谢谢
importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.util.Scanner;publicclassOutToTxt{;privatestaticScannersc;publicstaticvoidmain(String[]args){=newFile("./Out.txt");if(!out.exists()){try{out.createNewFile();}catch(IOExceptione){e.printStackTrace();}}try{writer=newBufferedWriter(newFileWriter(out));}catch(IOExceptione){e.printStackTrace();}sc=newScanner(System.in);System.out.println("请输入文本内容,输入exit结束:");try{writer.write("");//清空文本Stringsplit="";while(true){Stringline=sc.nextLine();if(line.equalsIgnoreCase("exit")){break;}writer.append(split+line);split="";}}catch(IOExceptione1){e1.printStackTrace();}finally{if(null!=writer){try{writer.flush();writer.close();}catch(IOExceptione){e.printStackTrace();}}}}}
你可以运行后输入
Hello World!
This is my first application.
exit
❾ java 数据输出到txt文件
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;public class TestBaiKnow {public static void main(String[] args) throws IOException {FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));PrintStream p = new PrintStream(fs);p.println(100);p.close();}}//简单的一个例子,来模拟输出
❿ java中怎么将内容输出到指定txt文件中
public static boolean writefile(String path,String content){答try{OutputStream out = new FileOutputStream(path);out.write(content.getBytes());out.close();return true;}catch(Exception e){e.printStackTrace();return false;} }
未经允许不得转载:山九号 » java输出内容到文件|怎么将一个java程序的结果输出到文本文档中写一段代码谢谢