当前位置: 技术问答>java相关
那位大哥有关于file 和dialog操作的例子?
来源: 互联网 发布时间:2015-09-30
本文导语: 就是用jbuilder编写关于 文件和文件处理(保存,打开)的例子? 3x | 不用JBuilder,这几个函数可以吗? public static String fileRead( String fileName ) { String buffer=""; FileInputStream fin = null; Buff...
就是用jbuilder编写关于 文件和文件处理(保存,打开)的例子?
3x
3x
|
不用JBuilder,这几个函数可以吗?
public static String fileRead( String fileName )
{
String buffer="";
FileInputStream fin = null;
BufferedReader in;
int more = 1;
File f = new File(fileName);
if (!f.exists()) {
System.out.println("file not found : " + fileName );
return buffer;
}
try
{
fin = new FileInputStream(f);
in = new BufferedReader(new InputStreamReader(fin));
while( more == 1 )
{ String s=null;
try
{
s = in.readLine();
}
catch ( Exception e )
{
System.out.println(e.toString());
}
if( s != null )
buffer += s + newline;
else
more = 0;
}
}
catch ( Exception e )
{
System.out.println(e.toString());
}
return buffer;
};
public static void fileSave(String fileName, String out)
{
byte[] writedata;
writedata=out.getBytes();
FileOutputStream outFile=null;
try
{
outFile = new FileOutputStream(fileName);
outFile.write(writedata);
outFile.close();
}
catch (Exception e)
{System.out.println(e.toString());}
}
public static void fileAppend(String fileName, String out)
{
byte[] writedata;
writedata=out.getBytes();
FileOutputStream outFile=null;
try {
outFile = new FileOutputStream(fileName, true);
outFile.write(writedata);
outFile.close();
}catch (Exception e){System.out.println(e.toString());}
}
public static String fileRead( String fileName )
{
String buffer="";
FileInputStream fin = null;
BufferedReader in;
int more = 1;
File f = new File(fileName);
if (!f.exists()) {
System.out.println("file not found : " + fileName );
return buffer;
}
try
{
fin = new FileInputStream(f);
in = new BufferedReader(new InputStreamReader(fin));
while( more == 1 )
{ String s=null;
try
{
s = in.readLine();
}
catch ( Exception e )
{
System.out.println(e.toString());
}
if( s != null )
buffer += s + newline;
else
more = 0;
}
}
catch ( Exception e )
{
System.out.println(e.toString());
}
return buffer;
};
public static void fileSave(String fileName, String out)
{
byte[] writedata;
writedata=out.getBytes();
FileOutputStream outFile=null;
try
{
outFile = new FileOutputStream(fileName);
outFile.write(writedata);
outFile.close();
}
catch (Exception e)
{System.out.println(e.toString());}
}
public static void fileAppend(String fileName, String out)
{
byte[] writedata;
writedata=out.getBytes();
FileOutputStream outFile=null;
try {
outFile = new FileOutputStream(fileName, true);
outFile.write(writedata);
outFile.close();
}catch (Exception e){System.out.println(e.toString());}
}
|
看看File.java , FileInputStream.java, FileOutputStream.java就差不多了
|
书上好像很多这些方面的例子啊。不太方便copy下来啦。不过一定有的。自己留心啦。
|
图形设计 卷II Swing 书上有FileChoose例子阿
核心技术 卷I 基础知识 上有文件和流例子
================================================================
核心技术 卷I 基础知识 上有文件和流例子
================================================================