BufferedWriter out = new BufferedWriter(new FileWriter(&quot什么意思;登黄鹤楼.txt&quot什么意思;));

他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Using same output stream to write to different ... - Software Development | DaniWeb
I am using the following code to write to the file out1.txt:
FileWriter fostream = new FileWriter("out1.txt");
BufferedWriter out = new BufferedWriter(fostream);
out.write("jlsdfjdlsfhsdkjf");
out.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
Now i want to write to multiple files in the same code (let us say 2 files).
Do i need to define a new object other than fostream to write to another file out2.txt or i could use the same object to write to out2.txt after i close it once?
I will try to explain it through a code perhaps:
FileWriter fostream = new FileWriter("out1.txt");
BufferedWriter out = new BufferedWriter(fostream);
out.write("dffjdlfjdklfjdklfdj");
out.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
FileWriter fostream = new FileWriter("out2.txt");
BufferedWriter out = new BufferedWriter(fostream);
out.write("gfgdgfdfg");
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
3Contributors 8Replies 31Views 4 YearsDiscussion Span
Last Post by JamesCherrill
This Question has been Answered
You are confusing variables and objects. Variables just contain references (pointers) to an object. You create new FileWriter and BufferedWriter objects each time. Because you only use one at a time it's perfectly OK to re-use the same variables to refer to those objects.
why don't you just do: FileWriter foS
foStream = new FileWriter("file1.txt");
catch(Exception e){
foStream = new FileWriter("file2.txt");
catch(Exception e){
you can also put them in the same try-block.
Yes, if we're discussing the try/catch, then with Java 7 or later you should be using a "try with reources" as being by far the easiest and safest way to ensure your streams are closed correctly under any/all circumstances, including when an exception is thrown. (And if you're not using Java 7 then you should be!) try ( FileWriter foStream = new FileWriter("file1.txt");
BufferedWriter out = new BufferedWriter(fostream)
out.write("gfgdgfdfg");
catch(Exception e){
e.printStackTrace();
Edited 4 Years Ago
by JamesCherrill
I have a parent file containig data alongwith dates. I am trying to sort my data into 12 files, one for each month. Therefore i am trying to create 12 output stream objects for each file. I will read the data from the parent file and will keep on writing the data in the corresponding file using the given output stream object for that file. My code says: '.class' expected in line where i am calling the crtoutobj function (in the for loop). import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;
public class cde{
public static void main(String args[]){
FileWriter fostream[] = new FileWriter[12];
BufferedWriter out[] = new BufferedWriter[12];
for(k=1;k&=12;k++){
filestr="out"+k+".txt";
crtoutobj(filestr, k, fostream[], out[]);
public static void crtoutobj(String s, int l, FileWriter gostream[],BufferedWriter rut[]){
gostream[l] = new FileWriter(s);
rut[l] = new BufferedWriter(fostream[l]);
System.out.println("Output stream for object"+" l"+" created");
}catch (Exception e){
System.out.println("Error in creating output stream for object "+l);
Could you please help me out with this error? Where am i going wrong?
@stultuske: I can use the multiple try blocks but i just wanted to make my program shorter by using a method like in my above post. This is always an option if nothing works out. :D
When you pass an array as a parameter you just use its name, you do not append a []
Oh. Ok. I am a newbie so please don't mind asking these kinds of doubts. :)
Most of the people posting in this forum are newbies, so please don't worry about asking questions!
This question has already been answered.
Have something to contribute to this discussion?
Please be thoughtful, detailed and courteous, and
be sure to adhere to our .
Post your Reply Alt+S
- 3 replies
- 5 replies
- 2 replies
- 4 replies
- 11 replies
- 3 replies
- 5 replies
- 3 replies
- 4 replies
- 24 replies
- 12 replies
- 5 replies
- 6 replies
- 4 replies
- 11 replies
- 2 replies
- 4 replies
- 10 replies
I have a bootstrap view which I am trying to get a modal working for. Unfortunately the modal is displaying as part of the page, not invisable. So it is ...
Its me again, wasnt sure if i should start a new thread so i just did :) I completly redid the code Now im having a problem with ...
Hello everyone and happy new year, i wonder is there something that allows you to detect that android device is near you? I want to create an app that will ...BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out))_百度知道
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out))
import java.io.*;
public class OutputStreamWriterTest {
public static void main(String[] args) throws IOException{
PrintStream ps = new PrintStream(&buffer.txt&);
String str =&OutputStreamWriterTestOutputStreamWriterTest&;//如何把...
我有更好的答案
为么还通过Sc;/如何把这个str输入到指定的buffer:\\OutputStreamWriterTestOutputStreamWriterTest&buffer.txt&).flush();
String str =&;&#47.txt文件夹中?就是如何把输出流的数据打印出来;
bw.write(str,0,44);
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(ps))输入输出流不是那么用的,你只是为了把这句话达到文件里.out转一下呢,public static void main(String[] args) throws IOException{
FileOutputStream ps = new FileOutputStream(&quot
还没理解他的用处,那str就是加入到输出流里了?buffer.txt没str的内容,就是说明str只是加入到输出流而已吗?
如果你非要这么干,可以吧System.setOut(ps); 放到BufferedWriter bw的声明前面,就是这样public static void main(String[] args) throws IOException{
PrintStream ps = new PrintStream(&c:\\buffer.txt&);
String str =&OutputStreamWriterTestOutputStreamWriterTest&;//如何把这个str输入到指定的buffer.txt文件夹中?就是如何把输出流的数据打印出来?
System.setOut(ps);
BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(str,0,44);
bw.flush();
bw.close();
要在BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out));之前设计System.setOut(ps);才行,谢谢!但在后面设置System.setOut(ps);行吗??就是设计System.setOut(ps);后还可以第二次设计System.setOut(ps);好像不行,感觉缓存区就只有一个干流,没有分流。
干流,分流,没明白你什么意思,不过你可以这么理解,System.setOut(ps),会把System.out设置成ps,注意是赋值,不是重指向,如果操作系统默认的输出认为是XPout对象,文件输出认为是FileOut对象,那么正常情况下存在System.out = XPBufferedWriter bw =new BufferedWriter(new OutputStreamWriter(System.out));这句话就是BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(XPout));执行System.setOut(ps);后果是System.out =而不是把XPout重指向到文件;
采纳率:37%
你要问什么?
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。使用BufferedReader和BufferedWriter按行读写文件
import java.io.*;
public class Test11 {
&private static BufferedReader br =
&private static File file =
&private static BufferedWriter bw =
&public static void main(String[] args) {
&&String b =
&&file = new
File("D:\\abc.txt");
&&if (!file.exists() != false)
&&&&file.createNewFile();
&&&} catch
(IOException e) {
&&&&e.printStackTrace();
&&&bw = new
BufferedWriter(new FileWriter(file));
&&&FileReader fr
= new FileReader("D:\\Log.txt");
&&&br = new
BufferedReader(fr);
&&&while ((b =
br.readLine()) != null) {
&&&&System.out.println(b);
&&&&bw.write(b);//输出字符串
&&&bw.newLine();//换行
&&&&bw.flush();
&&} catch (Exception e) {
&&&e.printStackTrace();
&&}finally {
&&&&br.close();
&&&&//bw.close();
&&&} catch
(IOException e) {
&&&&e.printStackTrace();
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 quot 转译回来 的文章

 

随机推荐