发布网友 发布时间:2022-05-14 03:21
共1个回答
热心网友 时间:2024-02-23 06:22
java">下面是我自己写的一个读取并显示txt文件的demo,希望对您有帮助。publicclassClient{publicstaticvoidmain(String[]args){ClientFramef=newClientFrame();}}importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Dimension;importjava.awt.GridLayout;importjava.awt.Toolkit;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.Socket;importjava.net.UnknownHostException;importjava.util.Vector;importjavax.swing.BorderFactory;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JList;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.event.ListSelectionEvent;importjavax.swing.event.ListSelectionListener;publicclassClientFrameextendsJFrameimplementsActionListener,ListSelectionListener{privateJListlist=null;privateJButtonsbtn=null;privateJButtoncbtn=null;privateVectorv=null;privateJTextAreatxt=null;privateContainercontrol=null;privateContainerbtn=null;privateSocketclient=null;privateDataInputStreamreader=null;privateDataOutputStreamwriter=null;publicClientFrame(){this.list=newJList();this.list.setBorder(BorderFactory.createTitledBorder("文件列表"));this.list.addListSelectionListener(this);this.sbtn=newJButton("显示");this.sbtn.addActionListener(this);this.cbtn=newJButton("清除");this.cbtn.addActionListener(this);this.control=newContainer();this.control.setPreferredSize(newDimension(150,400));this.control.setLayout(newBorderLayout());this.control.add(newJScrollPane(this.list),BorderLayout.CENTER);this.btn=newContainer();this.btn.setLayout(newGridLayout(1,2));btn.add(sbtn);btn.add(cbtn);this.control.add(this.btn,BorderLayout.SOUTH);this.txt=newJTextArea();this.txt.setEditable(false);this.txt.setSize(350,400);this.setTitle("客户端");this.setSize(500,400);this.setVisible(true);DimensiondisplaySize=Toolkit.getDefaultToolkit().getScreenSize();this.setLocation((displaySize.width-this.getWidth())/2,(displaySize.height-this.getHeight())/2);this.setLayout(newBorderLayout());this.add(this.control,BorderLayout.WEST);this.add(newJScrollPane(this.txt),BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);try{//this.client=newSocket("192.168.32.34",6666);this.client=newSocket("192.168.1.100",6666);this.reader=newDataInputStream(client.getInputStream());this.writer=newDataOutputStream(client.getOutputStream());}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicvoidactionPerformed(ActionEventevent){if(event.getSource()==sbtn){if(v==null){v=newVector();}else{v.clear();}try{writer.writeUTF("getfilelist");writer.flush();Stringt=reader.readUTF();while(t!=null&&!t.equals("")){v.add(t);t=reader.readUTF();}}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}this.list.setListData(v);}if(event.getSource()==cbtn){this.txt.setText("");}}publicvoidvalueChanged(ListSelectionEvente){inti=this.list.getSelectedIndex();if(!this.list.getValueIsAdjusting()&&i!=-1){try{writer.writeUTF("getfilecontent_"+i);writer.flush();Stringtmp=reader.readUTF();this.txt.setText(tmp);}catch(IOExceptione1){e1.printStackTrace();}}}}importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.Reader;importjava.net.ServerSocket;importjava.net.Socket;importjava.util.ArrayList;importjava.util.Iterator;publicclassServer{staticArrayListfileArray=newArrayList();publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;Socketclient=null;Stringcmd="";try{server=newServerSocket(6666);client=server.accept();DataInputStreamreader=newDataInputStream(client.getInputStream());DataOutputStreamwriter=newDataOutputStream(client.getOutputStream());while(true){cmd=reader.readUTF();System.out.println(cmd);if(cmd.equals("getfilelist")){fileArray.clear();//fileArray=getFile(newFile("D:/tmp"));fileArray=getFile(newFile("D:/学习/教程/学习笔记"));Stringfn="";for(intk=0;kgetFile(Filef){File[]ff=f.listFiles();for(Filechild:ff){if(child.isDirectory()){getFile(child);}else{fileArray.add(child);}}returnfileArray;}}追问您还在吗?您发的代码,小弟弄不了,报错!您能整理一下吗?我是新手,蒙圈了!万分感谢!