weixin_42301678 2009-03-22 16:01
浏览 162
已采纳

一段代码出了问题,关于文件输入输出的--------------大家帮忙看看

这是一段关于文件输入输出流的代码,首先弹出窗口输入要拷贝的文件名称,拷贝完成后会通过frame窗口告知拷贝信息,但是现在出了个问题,文件能够顺利拷贝,frame窗口能够打开,可是label却并不显示复制信息,给位帮忙看看问题出在哪里。我是JAVA初学者,所以问题找了半天也没找出来。谢谢各位帮忙啦!下面是代码:
[code="java"]
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.awt.*;
public class UpdateFile extends Frame
{
Label label1=new Label();
Label label2=new Label();
Label label3=new Label();
Label label4=new Label();
public void update(String fname,String childdir) throws IOException
{
File f1,f2,child;

f1 = new File(fname); //当前目录中创建文件对象f1
child = new File(childdir); //当前目录中创建文件对象child
if (f1.exists())
{
if (!child.exists()) //child不存在时创建子目录
{
child.mkdir();
}
f2 = new File(child,fname); //在子目录child中创建文件f2
if (!f2.exists()||f2.exists()&& //f2不存在时或存在但日期较早时
(f1.lastModified()>f2.lastModified()))
{
copy(f1,f2); //复制
}

init(f1);
init(child);
}
else
System.out.println(f1.getName()+" file not found!");
}
public void copy(File f1,File f2) throws IOException
{
FileInputStream rf = new FileInputStream(f1); //创建文件输入流对象
FileOutputStream wf = new FileOutputStream(f2); //创建文件输出流对象
int count,n=512;
byte buffer[] = new byte[n];
count = rf.read(buffer,0,n); //读取输入流
while (count != -1)
{
wf.write(buffer,0,count); //写入输出流
count = rf.read(buffer,0,n);
}
System.out.println("复制文件:"+f2.getName()+" !");
rf.close(); //关闭输入流
wf.close(); //关闭输出流
}
public void init(File f1) throws IOException
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
String gap=f1.getAbsolutePath();
String format=sdf.format(new Date(f1.lastModified()));
if (f1.isFile())
{
label1.setText(gap);
label2.setText("测试");
label3.setText(format);
}
else
{
label4.setText(gap);
File [] files=f1.listFiles();
for(int i=0;i<files.length;i++)
init(files[i]);
}
this.setLayout(null);
this.add(label1);
this.add(label2);
this.add(label3);
this.add(label4);
}

public static void main(String args[]) throws IOException 
{
    String fname;
    fname=JOptionPane.showInputDialog( "输入要复制的文件名:" );  //待复制的文件名
    String childdir = "backup";     //子目录名                  
    UpdateFile uf=new UpdateFile();
    uf.setSize(400, 300);
    uf.setTitle("文件拷贝信息!");
    uf.setVisible(true);
    uf.update(fname,childdir);
}

}
[/code]

  • 写回答

2条回答 默认 最新

  • danielzhan 2009-03-24 11:37
    关注

    首先init被调用两次,label也添加了两次,这样不好.

    不能现实的原因是第69行this.setLayout(null);
    在没有LayoutManager的情况下,由于无法排列component就会出现问题.
    这样就需要添加一个LayoutManager,如this.setLayout(new FlowLayout());
    在添加components完毕后也就是在73行以后要添加一个this.doLayout();来重新排列components.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果