41.对文件的访问
URL url = new URL("file:///C:/My Documents/Project1/counter.txt");
42.得到字符值,两个相减可得ascii码
char temp='0';
43.读取internet上的文件URL
1)String str=new String("");
try
{URL cdrom_catalog=new URL("http://www.cdrom.com/catalog.html");
DataInputStream dis;
String inputLine;
dis=new DataInputStream(cdrom_catalog.openStream());
while((inputLine=dis.readLine())!=null)
{str+=inputLine+"\n";
}
dis.close();
}
catch(MalformedURLException me)
{System.out .println ("MalformedURLException:"+me);
}
catch(IOException ioe)
{System.out.println("IOException:"+ioe); }2)只读取一个字符
private URL u=null;
u=new URL (“http://cn.yahoo.com”);
private InputStream InputStream input=u.openStream ();
try
{while(true)
{int nextch=is.read ();
if(nextch==-1)return false;//注意是和-1比较
if((char)nextch==ch)return true;
}
}
catch(IOException e)
{return false;}
///////////////////////////////////////////////////
44.读取文件
1) FileInputStream 工程目录下读取文件 只能读byte
注:String (byte[] byte,int offset,int length,string enc)
byte:数组;offset:起始位置;length:被创建的字符个数;enc:字符编码方式
public static void main(String[] args)
{int b;
byte buffer[]=new byte [2500];
try
{File f=new File ("target.txt");
FileInputStream readfile=new FileInputStream(f);
b=readfile.read (buffer,0,2500);
try
{String str=new String (buffer,0,b,"Default");
//将数组转换成字符串
System.out .println (str);
}
catch(UnsupportedEncodingException e)
{System.out.println ("the encoding was not found:"+e);
}
}
catch(IOException e)
{System.out.println ("File read Error");
}
}
/////////////////////////////////////////////////////
2)FileInputStream 指定目录下读取文件 只能读byte 较差
try
{File f=new File ("F:\\my_bag\\vj_file\\","进程.txt");
FileInputStream readfile=new FileInputStream(f);
while((b=readfile.read (tom,0,j))!=-1)
{String s=new String (tom,0,j);
System.out .println (s);
text.append(s+"\n");
}
readfile.close ();
}
catch(IOException e)
{System.out.println ("File read Error");
}
///////////////////////////////////////////////////
3)FileOutputStream 写入文件 只能写byte 较差
public static void main(String[] args)
{int b;
byte buffer[]=new byte [10];
try
{System.out.println ("输入一行文本,并存入磁盘:");
buffer[0]=48; buffer[1]=49; buffer[2]=50; buffer[3]=51; buffer[4]=52; buffer[5]=53; buffer[6]=54; buffer[7]=55;
buffer[8]=56; buffer[9]=57;
b=buffer.length;
FileOutputStream writefile=new FileOutputStream ("write_in_here.txt");
writefile.write(buffer,0,b);
}
catch(IOException e)
{System.out.println ("error");
}
}
////////////////////////////////////////////
4)读文件 FileReader 以行为单位读 较好
BufferedReader in;
FileReader file;
try
{File f=new File ("F:\\my_bag\\vj_file\\读文件\\","target.htm");
file=new FileReader(f);
in=new BufferedReader (file);
}
catch(FileNotFoundException e){}
-----------------
public void actionPerformed(ActionEvent e)
{String s;
if(e.getSource ()==button)
try
{while((s=in.readLine ())!=null)
text.append (s+'\n');
}
catch(IOException exp){}
}
}
///////////////////////////////////////////////////
5)写文件 FileWriter 以行为单位写 较好
BufferedWriter out;
FileWriter tofile;
try
{tofile=new FileWriter ("write_in_here.txt");
out=new BufferedWriter (tofile);
}
catch(FileNotFoundException e){}
catch(IOException e){}
addWindowListener (new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible (false);
System.exit (0);}
});
}
public void actionPerformed(ActionEvent e)
{String s;
if(e.getSource ()==button_me)
try
{out.write(text.getText (),0,(text.getText()).length ());
out.flush ();
}
catch(IOException exp){text.setText ("have problem.");}
}
}//////////////////////////////////////////////////////////
6)RandomAccessFile 可以随机读取
public class RandomAccessFile类
{
public static void main(String[] args)
{RandomAccessFile in_and_out=null;
int data[]={124,389,33,256,-90,34,21,7,100,25};
try
{in_and_out=new RandomAccessFile ("write_in_here.txt","rw");
}
catch(FileNotFoundException e)
{System.out .println ("??????");
}
catch(IOException e)
{System.out .println ("??????");
}
try
{for(int i=0;i<data.length;i++)
in_and_out.writeInt(data[i]);
for(int i=data.length -1;i>=0;i--)
{in_and_out.seek (i*4);
System.out .print (","+in_and_out.readInt ());
}
in_and_out.close ();
}
catch(IOException e)
{System.out .println ("??????");
}
}
}7)DataInputStream 对字符逐个读取
读取
try
{File file=new File(filedialog_load.getDirectory(),filedialog_load.getFile());
file_read=new FileInputStream(file);
in_data=new DataInputStream (file_read);
label.setText (filedialog_load.getDirectory().toString ()+filedialog_load.getFile().toString() );
int i=0;
while((s=in_data.readUTF ())!=null)
{name[i].setText (s);result[i].setText (""+in_data.readDouble ());
i++;
}
}
catch(FileNotFoundException e1){}
catch (IOException e2){}
try
{in_data.close ();
file_read.close ();
}
catch (IOException e2){}
}
----------------------------------------------
存入
try
{File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileOutputStream (file);
out_data=new DataOutputStream (tofile);
for(int i=0;i<=29;i++)
{str=name[i].getText();
out_data.writeUTF (name[i].getText ());//写入String
out_data.writeDouble (Double.valueOf (result[i].getText ()).doubleValue ());
}
}
catch(FileNotFoundException e1){}
catch (IOException e2){}
try
{out_data.close ();
tofile.close ();
}
catch (IOException e2){}
}
///////////////////////////////////////////
这是我的笔记,我想对你一定有点用
URL url = new URL("file:///C:/My Documents/Project1/counter.txt");
42.得到字符值,两个相减可得ascii码
char temp='0';
43.读取internet上的文件URL
1)String str=new String("");
try
{URL cdrom_catalog=new URL("http://www.cdrom.com/catalog.html");
DataInputStream dis;
String inputLine;
dis=new DataInputStream(cdrom_catalog.openStream());
while((inputLine=dis.readLine())!=null)
{str+=inputLine+"\n";
}
dis.close();
}
catch(MalformedURLException me)
{System.out .println ("MalformedURLException:"+me);
}
catch(IOException ioe)
{System.out.println("IOException:"+ioe); }2)只读取一个字符
private URL u=null;
u=new URL (“http://cn.yahoo.com”);
private InputStream InputStream input=u.openStream ();
try
{while(true)
{int nextch=is.read ();
if(nextch==-1)return false;//注意是和-1比较
if((char)nextch==ch)return true;
}
}
catch(IOException e)
{return false;}
///////////////////////////////////////////////////
44.读取文件
1) FileInputStream 工程目录下读取文件 只能读byte
注:String (byte[] byte,int offset,int length,string enc)
byte:数组;offset:起始位置;length:被创建的字符个数;enc:字符编码方式
public static void main(String[] args)
{int b;
byte buffer[]=new byte [2500];
try
{File f=new File ("target.txt");
FileInputStream readfile=new FileInputStream(f);
b=readfile.read (buffer,0,2500);
try
{String str=new String (buffer,0,b,"Default");
//将数组转换成字符串
System.out .println (str);
}
catch(UnsupportedEncodingException e)
{System.out.println ("the encoding was not found:"+e);
}
}
catch(IOException e)
{System.out.println ("File read Error");
}
}
/////////////////////////////////////////////////////
2)FileInputStream 指定目录下读取文件 只能读byte 较差
try
{File f=new File ("F:\\my_bag\\vj_file\\","进程.txt");
FileInputStream readfile=new FileInputStream(f);
while((b=readfile.read (tom,0,j))!=-1)
{String s=new String (tom,0,j);
System.out .println (s);
text.append(s+"\n");
}
readfile.close ();
}
catch(IOException e)
{System.out.println ("File read Error");
}
///////////////////////////////////////////////////
3)FileOutputStream 写入文件 只能写byte 较差
public static void main(String[] args)
{int b;
byte buffer[]=new byte [10];
try
{System.out.println ("输入一行文本,并存入磁盘:");
buffer[0]=48; buffer[1]=49; buffer[2]=50; buffer[3]=51; buffer[4]=52; buffer[5]=53; buffer[6]=54; buffer[7]=55;
buffer[8]=56; buffer[9]=57;
b=buffer.length;
FileOutputStream writefile=new FileOutputStream ("write_in_here.txt");
writefile.write(buffer,0,b);
}
catch(IOException e)
{System.out.println ("error");
}
}
////////////////////////////////////////////
4)读文件 FileReader 以行为单位读 较好
BufferedReader in;
FileReader file;
try
{File f=new File ("F:\\my_bag\\vj_file\\读文件\\","target.htm");
file=new FileReader(f);
in=new BufferedReader (file);
}
catch(FileNotFoundException e){}
-----------------
public void actionPerformed(ActionEvent e)
{String s;
if(e.getSource ()==button)
try
{while((s=in.readLine ())!=null)
text.append (s+'\n');
}
catch(IOException exp){}
}
}
///////////////////////////////////////////////////
5)写文件 FileWriter 以行为单位写 较好
BufferedWriter out;
FileWriter tofile;
try
{tofile=new FileWriter ("write_in_here.txt");
out=new BufferedWriter (tofile);
}
catch(FileNotFoundException e){}
catch(IOException e){}
addWindowListener (new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible (false);
System.exit (0);}
});
}
public void actionPerformed(ActionEvent e)
{String s;
if(e.getSource ()==button_me)
try
{out.write(text.getText (),0,(text.getText()).length ());
out.flush ();
}
catch(IOException exp){text.setText ("have problem.");}
}
}//////////////////////////////////////////////////////////
6)RandomAccessFile 可以随机读取
public class RandomAccessFile类
{
public static void main(String[] args)
{RandomAccessFile in_and_out=null;
int data[]={124,389,33,256,-90,34,21,7,100,25};
try
{in_and_out=new RandomAccessFile ("write_in_here.txt","rw");
}
catch(FileNotFoundException e)
{System.out .println ("??????");
}
catch(IOException e)
{System.out .println ("??????");
}
try
{for(int i=0;i<data.length;i++)
in_and_out.writeInt(data[i]);
for(int i=data.length -1;i>=0;i--)
{in_and_out.seek (i*4);
System.out .print (","+in_and_out.readInt ());
}
in_and_out.close ();
}
catch(IOException e)
{System.out .println ("??????");
}
}
}7)DataInputStream 对字符逐个读取
读取
try
{File file=new File(filedialog_load.getDirectory(),filedialog_load.getFile());
file_read=new FileInputStream(file);
in_data=new DataInputStream (file_read);
label.setText (filedialog_load.getDirectory().toString ()+filedialog_load.getFile().toString() );
int i=0;
while((s=in_data.readUTF ())!=null)
{name[i].setText (s);result[i].setText (""+in_data.readDouble ());
i++;
}
}
catch(FileNotFoundException e1){}
catch (IOException e2){}
try
{in_data.close ();
file_read.close ();
}
catch (IOException e2){}
}
----------------------------------------------
存入
try
{File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileOutputStream (file);
out_data=new DataOutputStream (tofile);
for(int i=0;i<=29;i++)
{str=name[i].getText();
out_data.writeUTF (name[i].getText ());//写入String
out_data.writeDouble (Double.valueOf (result[i].getText ()).doubleValue ());
}
}
catch(FileNotFoundException e1){}
catch (IOException e2){}
try
{out_data.close ();
tofile.close ();
}
catch (IOException e2){}
}
///////////////////////////////////////////
这是我的笔记,我想对你一定有点用
FileInputStream file = new FileInputStream("xx.txt");
BufferedReader bReader = new BufferedReader(new InputStreamReader(file));
String message;
while((message = bReader.readLine())!= null){
System.out.println(message;
}
bReader.close();
file.close();
}catch(IOException e){
}