我可以在Java的同一类中编写FileWriter和bufferedwriter吗?

时间:2018-11-09 04:33:33

标签: java filewriter bufferedwriter

这是代码,我正在尝试使用Filewriter进行编写。一切正常。

File f2 = new File("Path");
f2.createNewFile();
FileWriter writing = new FileWriter(f2);    
writing.write("i'm into you , i'm into you");   
writing.flush(); 

在下面的代码中,我试图使用bufferedwriter进行编写。这不会在同一文件中添加任何文本。对于其他文件,它可以正常工作。

BufferedWriter buffwrite = new BufferedWriter(writing);     buffwrite.write("java");    
writing.flush(); 

4 个答案:

答案 0 :(得分:1)

是的,绝对可以在同一个类中编写两者。 您的代码未在同一文件中添加任何文本,因为您是在BufferedWriter之前清除FileWriter。我只是按如下方式编辑了您的代码,效果很好。

File f2 = new File("Path");
f2.createNewFile();
FileWriter writing = new FileWriter(f2);  
writing.write("i'm into you , i'm into you");

BufferedWriter buffwrite = new BufferedWriter(writing); 
buffwrite.write("java"); 

buffwrite.flush();//flush BufferedWriter first followed by FileWriter
writing.flush(); 

答案 1 :(得分:0)

这些与IO相关的类是基于装饰器模式设计的。

如果您引用BufferedWriter类javadoc,则会发现一个带有Writer类型对象的构造函数。 Writer是一个抽象类,由FileWriter扩展了其他类。在构造函数中传递FileWriter对象,然后调用write (...)的{​​{1}}和flush方法。

所有IO类均以这种模式工作。

答案 2 :(得分:0)

,您可以编写。请在使用Java中使用FileWriter,BufferedWriter,FileOutputStream和Files签出以下用Java编写文件的用例。

$(function(){
    var numItems = $('.isFormInput').length;

    var ts = 0;
    $('.isFormInput').each(function(i, obj) {
        /*
        * Get the div and select it
        */
        var theTargetDiv = $(this).attr('id');
        var theTargetDivSelector = "."+theTargetDiv;
            theTargetDivSelector = $(theTargetDivSelector);
        /*
        * build the divs and append to container div "dashboard-content"
        */
        var g = document.createElement('div');
        g.setAttribute('class', "boxMeW " + theTargetDiv + "_w");
        document.getElementById("dashboard-content").appendChild(g);
        var e = document.createElement('div');
        e.setAttribute('class', "boxMeH " + theTargetDiv + "_h");
        document.getElementById("dashboard-content").appendChild(e);
        /*
        * build and get positions/sizes
        */
        var h1W = theTargetDivSelector.innerWidth();
        var h1H = theTargetDivSelector.innerHeight();
        var leftPos  = theTargetDivSelector.offset().left;
        var rightPos = theTargetDivSelector.right  + $(window)['scrollLeft']();
        var topPos   =  theTargetDivSelector.offset().top;
        var bottomPos= theTargetDivSelector.bottom + $(window)['scrollTop']();
        /*
        * Create the on the fly css for the targeted divs
        */
        var adjust_for_side_bar = $( "th" ).first().innerWidth();
        var adjust_for_header = $( ".bgnav" ).first().innerHeight();
        //var hey = $( "th" ).first().innerWidth();
        $("." + theTargetDiv + "_w").css({
            height: h1H,
            top:topPos-adjust_for_header
        }).hide();;
        $("." + theTargetDiv + "_h").css({
            width: h1W,
            left:leftPos-adjust_for_side_bar
        }).hide();;
    });
}); 

答案 3 :(得分:0)

您应该IN6ADDR_ANY_INIT closeFileWriter writing这是您打开的资源。这个(JavaDoc

  

刷新流。如果流中已保存来自   缓冲区中的各种write()方法,将它们立即写入它们的   预定目的地。然后,如果那个目的地是另一个字符   或字节流,将其刷新。因此,一个flush()调用将刷新所有   在Writer和OutputStreams链中的缓冲区。

正确的方法是使用try-resource语句。这将有助于关闭已打开的资源。

或使用BufferedWriter buffwrite方法来进行资源处理。