原理的话,参考你用浏览器打开这个链接显示的内容。
这个返回的是一个HTML网页,需要你解析出里面的文字(一般来说取body中间的内容就行)
其实对于这种文件一般用FTP来下载的。楼上写的那个不对,哈哈。
需要的话自己最好去查一下,怎么用,我有代码,含斗烂不过告诉你的谈漏话也不太好?
URL url = new URL("http://你的地址")
URLConnection connection = url.openConnection()
InputStream is = connection.getInputStream()
BufferedReader br = new BufferedReader(new InputStreamReader(is,"gb2312"))
下面就是解析这个字符串来,自销森己来吧
Java源程序:importjava.awt.*importjava.awt.event.*importjavax.swing.*importjava.io.*publicclassGUIRWFileextendsJFrameimplementsActionListener{JTextAreatxtMessageJButtonbtnRead,btnWritepublicGUIRWFile(){super("文唤歼本读写判肢")txtMessage=newJTextArea(15,30)btnRead=newJButton("读取")btnWrite=newJButton("写入")this.setLayout(newFlowLayout())this.add(newJLabel("文件内容:"))this.add(txtMessage)this.add(btnRead)this.add(btnWrite)btnRead.addActionListener(this)btnWrite.addActionListener(this)this.setSize(400,400)this.setVisible(true)this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)}publicstaticvoidmain(String[]args){newGUIRWFile()}@OverridepublicvoidactionPerformed(ActionEvente){JButtonbtn=(JButton)e.getSource()StringfileName="D:\\test.txt"if(btn.getText().equals("读取")){try{InputStreamis=newFileInputStream(fileName)BufferedReaderbr=newBufferedReader(newInputStreamReader(is))Stringline=nullStringBufferbuffer=newStringBuffer()while((line=br.readLine())!=null){buffer.append(line+"\n")}txtMessage.setText(buffer.toString())br.close()is.close()}catch(Exceptionex){JOptionPane.showMessageDialog(this,"系统I/O错误!")}}elseif(btn.getText().equals("写入")){try{Stringmsg=txtMessage.getText()OutputStreamos=newFileOutputStream(fileName)PrintStreamps=newPrintStream(os)ps.print(msg)ps.close()os.close()}catch(Exceptionex){JOptionPane.showMessageDialog(this,"系统I/O错误!掘链世")}}}}运行测试:欢迎分享,转载请注明来源:内存溢出
评论列表(0条)