当前位置: 技术问答>java相关
java文件类使用示例怎么不能把文件读出来呢?
来源: 互联网 发布时间:2015-04-03
本文导语: 请看源码 import java.io.*; import java.awt.*; import java.applet.*; public class fileapp1 extends Applet { byte buffer[] = new byte[ 5120]; int bytes; //create a area for text TextArea hArea = new TextArea("",14,70); public void init(){ this.add(h...
请看源码
import java.io.*;
import java.awt.*;
import java.applet.*;
public class fileapp1 extends Applet
{
byte buffer[] = new byte[ 5120];
int bytes;
//create a area for text
TextArea hArea = new TextArea("",14,70);
public void init(){
this.add(hArea);
}
public void paint(Graphics g) {
try
{
//Create a FileInputStream object
FileInputStream fileIn = new FileInputStream("MyArrayApplet.java");
//Read byte arrary
bytes = fileIn.read(buffer,0,5120);
//Create a string
String hStr = new String(buffer,0,0,bytes);
//Show Context
hArea.appendText(hStr);
fileIn.close();
}
catch (Exception e)
{
hArea.appendText("Reading file Error "+"n");
}
try
{
//Create a FileOutPutStream
FileOutputStream fileOut = new FileOutputStream("temp.txt");
fileOut.write(buffer,0,bytes);
fileOut.close();
}
catch (Exception e)
{
hArea.appendText("Writting file Error"+"n");
}
}
}
import java.io.*;
import java.awt.*;
import java.applet.*;
public class fileapp1 extends Applet
{
byte buffer[] = new byte[ 5120];
int bytes;
//create a area for text
TextArea hArea = new TextArea("",14,70);
public void init(){
this.add(hArea);
}
public void paint(Graphics g) {
try
{
//Create a FileInputStream object
FileInputStream fileIn = new FileInputStream("MyArrayApplet.java");
//Read byte arrary
bytes = fileIn.read(buffer,0,5120);
//Create a string
String hStr = new String(buffer,0,0,bytes);
//Show Context
hArea.appendText(hStr);
fileIn.close();
}
catch (Exception e)
{
hArea.appendText("Reading file Error "+"n");
}
try
{
//Create a FileOutPutStream
FileOutputStream fileOut = new FileOutputStream("temp.txt");
fileOut.write(buffer,0,bytes);
fileOut.close();
}
catch (Exception e)
{
hArea.appendText("Writting file Error"+"n");
}
}
}
|
最大的可能是Applet的安全性问题。
用Frame而不用Applet来实现文件操作试试看。
用Frame而不用Applet来实现文件操作试试看。
|
我给你个例子
private java.util.Vector getMethod(String sFileNm) {
java.io.BufferedReader looReader = null;
try {
looReader = new java.io.BufferedReader(new java.io.FileReader(sFileNm));
} catch(FileNotFoundException evt) {
}
String sLine = "";
String sLine2 = "";
classMethod = new java.util.Vector(10,10);
try {
while ((sLine2 = looReader.readLine()) != null) {
sLine2 = sLine2.trim();
if (sLine2.indexOf("public ") == 0 && sLine2.indexOf(" class ") 0) {
sLine = sLine2.substring(0,sLine2.indexOf("{"));
classMethod.add(sLine);
sLine = "";
} else {
sLine = sLine2;
}
} else {
if (sLine.indexOf("public ") == 0) {
if (sLine2.indexOf("{") > 0) {
sLine = sLine + " " + sLine2.substring(0,sLine2.indexOf("{"));
classMethod.add(sLine);
sLine = "";
} else {
sLine = sLine + " " + sLine2;
}
} else {
sLine = "";
}
}
}
classMethod.trimToSize();
looReader.close();
}catch(IOException evt) {
}
return classMethod;
}
private java.util.Vector getMethod(String sFileNm) {
java.io.BufferedReader looReader = null;
try {
looReader = new java.io.BufferedReader(new java.io.FileReader(sFileNm));
} catch(FileNotFoundException evt) {
}
String sLine = "";
String sLine2 = "";
classMethod = new java.util.Vector(10,10);
try {
while ((sLine2 = looReader.readLine()) != null) {
sLine2 = sLine2.trim();
if (sLine2.indexOf("public ") == 0 && sLine2.indexOf(" class ") 0) {
sLine = sLine2.substring(0,sLine2.indexOf("{"));
classMethod.add(sLine);
sLine = "";
} else {
sLine = sLine2;
}
} else {
if (sLine.indexOf("public ") == 0) {
if (sLine2.indexOf("{") > 0) {
sLine = sLine + " " + sLine2.substring(0,sLine2.indexOf("{"));
classMethod.add(sLine);
sLine = "";
} else {
sLine = sLine + " " + sLine2;
}
} else {
sLine = "";
}
}
}
classMethod.trimToSize();
looReader.close();
}catch(IOException evt) {
}
return classMethod;
}
|
BufferedReader in=new BufferedReader(new FileReader(fileName));
|
你用的JDK版本超过1.1
到sun的网站下载
Java(tm) Plug-in HTML Converter
然后将你的html等转换即可
到sun的网站下载
Java(tm) Plug-in HTML Converter
然后将你的html等转换即可