Hi, ich hab hier ein Code Fragment, mit dem ich eine "Txt" datei aus dem Internet lade, aber es klappt nicht ihren inhalt zu laden,
wieso?
wieso?
Code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Ex extends JFrame
{
//https://java-rayman.dev.java.net/files/documents/9616/130127/Chat.txt
public Chat()
{
setVisible(true);
setSize(250,500);
setLayout(null);
final JTextArea time = new JTextArea();time.setBounds(0,0,250,500); add(time);
(new Thread()
{
@Override public void run()
{
while(!isInterrupted())
{
File a = null;
try {
a = new File(new URI("https://java-rayman.dev.java.net/files/documents/9616/130127/Chat.txt"));
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
time.setText((getContents(a)).toString());
try{
sleep(100);
}catch(InterruptedException e){
interrupt();
}
}
}
}).start();
}
public static void main(String[]args)
{
new Ex();
}
static public String getContents(File aFile) {
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
static public void setContents(File aFile, String aContents)
throws FileNotFoundException, IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: " + aFile);
}
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: " + aFile);
}
Writer output = new BufferedWriter(new FileWriter(aFile));
try {
output.write( aContents );
}
finally {
output.close();
}
}
}