当前位置: 技术问答>java相关
为什么读文件失败?
来源: 互联网 发布时间:2015-08-04
本文导语: 下面是我的程序: import java.io.*; public class FileApp { public static void main (String args[]) { byte buffer[]=new byte[2500]; try { FileInputStream fileIn=new FileInputStre...
下面是我的程序:
import java.io.*;
public class FileApp {
public static void main (String args[])
{
byte buffer[]=new byte[2500];
try
{
FileInputStream fileIn=new FileInputStream("newbook1.txt");
int bytes=fileIn.read(buffer,0,2500);
try
{
String str=new String(buffer,0,bytes,"Default");
System.out.println(str);
}
catch(UnsupportedEncodingException e)
{
System.out.println("The encoding"Default"was not found:"+e);
}
fileIn.close();
}
catch(Exception e)
{
String err=e.toString();
System.out.println("error:"+err);
}
}
}
用浏览器打开后总会提示:
java.lang.ClassCastException: FileApp
at sun.applet.AppletPanel.createApplet(AppletPanel.java:548)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1621)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:477)
at sun.applet.AppletPanel.run(AppletPanel.java:290)
at java.lang.Thread.run(Thread.java:536)
import java.io.*;
public class FileApp {
public static void main (String args[])
{
byte buffer[]=new byte[2500];
try
{
FileInputStream fileIn=new FileInputStream("newbook1.txt");
int bytes=fileIn.read(buffer,0,2500);
try
{
String str=new String(buffer,0,bytes,"Default");
System.out.println(str);
}
catch(UnsupportedEncodingException e)
{
System.out.println("The encoding"Default"was not found:"+e);
}
fileIn.close();
}
catch(Exception e)
{
String err=e.toString();
System.out.println("error:"+err);
}
}
}
用浏览器打开后总会提示:
java.lang.ClassCastException: FileApp
at sun.applet.AppletPanel.createApplet(AppletPanel.java:548)
at sun.plugin.AppletViewer.createApplet(AppletViewer.java:1621)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:477)
at sun.applet.AppletPanel.run(AppletPanel.java:290)
at java.lang.Thread.run(Thread.java:536)
|
你这是简单java程序,故你只需要在IDE中run或dos下 java运行就可以了
浏览器是运行applet的
你这个不是applet
所以不行
浏览器是运行applet的
你这个不是applet
所以不行
|
import java.io.*;
import java.awt.*;
import java.applet.*;
public class FileApp extends Applet {
public void init() {
byte buffer[]=new byte[2500];
try
{
FileInputStream fileIn=new FileInputStream("newbook1.txt");
int bytes=fileIn.read(buffer,0,2500);
try
{
String str=new String(buffer,0,bytes,"Default");
System.out.println(str);
}
catch(UnsupportedEncodingException e)
{
System.out.println("The encoding"Default"was not found:"+e);
}
fileIn.close();
}
catch(Exception e)
{
String err=e.toString();
System.out.println("error:"+err);
}
}}
import java.awt.*;
import java.applet.*;
public class FileApp extends Applet {
public void init() {
byte buffer[]=new byte[2500];
try
{
FileInputStream fileIn=new FileInputStream("newbook1.txt");
int bytes=fileIn.read(buffer,0,2500);
try
{
String str=new String(buffer,0,bytes,"Default");
System.out.println(str);
}
catch(UnsupportedEncodingException e)
{
System.out.println("The encoding"Default"was not found:"+e);
}
fileIn.close();
}
catch(Exception e)
{
String err=e.toString();
System.out.println("error:"+err);
}
}}