📄
字号:
20-例子1
import java.io.*;
class Example20_1
{ public static void main(String args[])
{ int b;
byte buffer[]=new byte[2500];
try{ FileInputStream readfile=new FileInputStream("Example211.java");
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 ");
}
}
}
20-例子2
import java.io.*;import java.awt.*;import java.awt.event.*;
class Example20_2
{ static Frame f;
public static void main(String args[])
{ int b,j=10; TextArea text;
byte tom[]=new byte[2500];f=new Frame();
f.setSize(100,100);text=new TextArea(30,60);
f.setVisible(true);f.add(text); f.pack();
f.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{f.setVisible(false);System.exit(0);}
});
try{ File f=new File("E:\\dd\\","AA.java");
FileInputStream readfile=new FileInputStream(f);
while((b=readfile.read(tom,0,j))!=-1)
{String s=new String (tom,0,b);
System.out.println(s);
text.append(s);
}
readfile.close();
}
catch(IOException e)
{System.out.println("File read Error ");
}
}
}
20-例子3
import java.io.*;
class Example20_3
{ public static void main(String args[])
{ int b;
byte buffer[]=new byte[100];
try{System.out.println("输入一行文本,并存入磁盘:");
b=System.in.read(buffer);//把从键盘敲入的字符存入buffer。
FileOutputStream whritefile=new FileOutputStream("line.txt");
whritefile.write(buffer,0,b);// 通过流把 buffer写入到文件line.txt。
}
catch(IOException e)
{System.out.println("Error ");
}
}
}
20-例子4
import java.applet.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class EWindow extends Frame implements ActionListener
{ TextArea text; BufferedReader in; Button button;
FileReader file;
EWindow()
{super("流的读取");
text=new TextArea(10,10);text.setBackground(Color.cyan);
try {File f=new File("e:/dd/","E2.html");
file=new FileReader(f);
in=new BufferedReader(file);
}
catch(FileNotFoundException e){}
catch(IOException e){}
button =new Button("读取");
button.addActionListener(this);
setLayout(new BorderLayout());
setSize(40,40);
setVisible(true);
add(text,"Center");add("South",button);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0); }
});
}
public void actionPerformed(ActionEvent e)
{ String s;
if(e.getSource()==button)
try{
while((s=in.readLine())!=null)
text.append(s+'\n');
}
catch(IOException exp){}
}
}
public class Example20_4
{public static void main(String args[])
{EWindow w=new EWindow();w.pack();
}
}
20-例子5
import java.applet.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class FWindow extends Frame implements ActionListener
{ TextArea text; BufferedWriter out; Button button;
FileWriter tofile;
FWindow()
{super("流的写入");
text=new TextArea(10,10);text.setBackground(Color.cyan);
try {tofile=new FileWriter("hello.txt");
out=new BufferedWriter(tofile);
}
catch(FileNotFoundException e){}
catch(IOException e){}
button =new Button("写入");
button.addActionListener(this);
setLayout(new BorderLayout());
setSize(60,70);
setVisible(true);
add(text,"Center");add("South",button);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0); }
});
}
public void actionPerformed(ActionEvent e)
{ String s;
if(e.getSource()==button)
try {
out.write(text.getText(),0,(text.getText()).length());
out.flush();
}
catch(IOException exp){System.out.println(" have problem");}
}
}
public class Example20_5
{public static void main(String args[])
{FWindow w=new FWindow();w.pack();//用紧凑方式显示窗口
}
}
20-例子6
import java.util.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
class EWindow extends Frame implements ItemListener,ActionListener
{ String str[]=new String[7];String s;
FileReader file;BufferedReader in; Button button_start;
Checkbox box[];TextField text_word,text_fenshu;
int fenshu=0; CheckboxGroup age=new CheckboxGroup();
EWindow()
{super("英语单词学习");
text_fenshu=new TextField(10);text_word=new TextField(70);
button_start=new Button("重新练习");button_start.addActionListener(this);
try {file=new FileReader("English.txt");
in=new BufferedReader(file);
}
catch(FileNotFoundException e){}
catch(IOException e){}
setBounds(100,100,400,320); setVisible(true);
setLayout(new GridLayout(5,1));setBackground(Color.pink);
Panel p1=new Panel(),p2=new Panel(),p3=new Panel() ,p4=new Panel();
p2.setLayout(new GridLayout(2,1)); p3.setLayout(new GridLayout(5,1));
p1.add(new Label("您的得分:"));p1.add(text_fenshu);
p2.add(new Label("句子填空:"));p2.add(text_word);
p4.add(button_start); box=new Checkbox[6];
for(int i=0;i<=4;i++)
{box[i]=new Checkbox(" ",false,age);
box[i].addItemListener(this);
}
p3.add(new Label("选择答案:"));
for(int i=1;i<=4;i++)
{p3.add(box[i]);}
add(p1); add(p2);add(new Label()); add(p3);add(p4);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0); }
});
reading();
}
public void reading()
{int i=0;
try{ s=in.readLine();
if(!(s.startsWith("endend")))
{StringTokenizer tokenizer=new StringTokenizer(s,"#");
while(tokenizer.hasMoreTokens())
{str[i]=tokenizer.nextToken();i++;
}
text_word.setText(str[0]);
for(int j=1;j<=4;j++)
{box[j].setLabel(str[j]);
}
}
else if(s.startsWith("endend"))
{ text_word.setText("学习完毕");
for(int j=1;j<=4;j++)
{box[j].setLabel("end"); in.close();file.close();
}
}
}
catch(IOException exp){}
}
public void actionPerformed(ActionEvent event)
{if(event.getSource()==button_start)
{file=null;in=null;fenshu=0;text_fenshu.setText("得分: "+fenshu);
try {file=new FileReader("English.txt");
in=new BufferedReader(file);
}
catch(FileNotFoundException e){}
catch(IOException e){}
reading();
}
}
public void itemStateChanged(ItemEvent e)
{ for(int j=1;j<=4;j++)
{ if(box[j].getLabel().equals(str[5])&&box[j].getState())
{fenshu++;text_fenshu.setText("得分: "+fenshu);
}
}
reading();
}
}
public class Example20_6
{public static void main(String args[])
{EWindow w=new EWindow();w.pack();
}
}
20-例子7
import java.awt.*;import java.io.*;
import java.awt.event.*;
public class Example20_7
{public static void main(String args[])
{ Frame_FileDialog f=new Frame_FileDialog(); }
}
class Frame_FileDialog extends Frame implements ActionListener
{ FileDialog filedialog_save,filedialog_load;//声明2个文件对话框
MenuBar menubar;Menu menu;MenuItem item1,item2;TextArea text;
BufferedReader in;
FileReader file_reader;
BufferedWriter out;
FileWriter tofile;
Frame_FileDialog()
{ super("带文件对话框的窗口");
setSize(60,70);
setVisible(true);
menubar=new MenuBar();
menu=new Menu("文件");
item1=new MenuItem("打开文件");
item2=new MenuItem("保存文件");
item1.addActionListener(this);
item2.addActionListener(this);
menu.add(item1);
menu.add(item2);
menubar.add(menu);
setMenuBar(menubar);
//下面创建1个依赖于该窗口的保存文件对话框
filedialog_save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);
filedialog_save.setVisible(false);
//再创建1个依赖于该窗口的打开文件对话框
filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD);
filedialog_load.setVisible(false);
filedialog_save.addWindowListener(new WindowAdapter()//对话框增加适配器
{public void windowClosing(WindowEvent e)
{filedialog_save.setVisible(false);}
});
filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器
{public void windowClosing(WindowEvent e)
{filedialog_load.setVisible(false);}
});
addWindowListener(new WindowAdapter() //窗口增加适配器
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0);}
});
text=new TextArea(10,10);text.setBackground(Color.cyan);
add(text,"Center");
}
public void actionPerformed(ActionEvent e) //实现接口中的方法
{ if(e.getSource()==item1)
{ filedialog_load.setVisible(true);String s;
try {//建立到文件file的FileReader流,该文件通过File类和对话框来确定
File file=new File(filedialog_load.getDirectory(),filedialog_load.getFile());
file_reader=new FileReader(file);
in=new BufferedReader(file_reader);
while((s=in.readLine())!=null)
text.append(s+'\n');
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
try {in.close();
file_reader.close();
}
catch(IOException exp){}
}
else if(e.getSource()==item2)
{ filedialog_save.setVisible(true);
try {//建立到文件file的FileWtiter流,该文件通过File类和对话框来确定
File file=new File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.flush();
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
try {out.close();
tofile.close();
}
catch(IOException exp){}
}
}
}
20-例子8
import java.awt.*;import java.io.*;
import java.awt.event.*;
public class Example20_8
{public static void main(String args[])
{ Frame_FileDialog f=new Frame_FileDialog(); }
}
class Frame_FileDialog extends Frame implements ActionListener
{ FileDialog filedialog_load;//声明文件对话框
MenuBar menubar;Menu menu;MenuItem item;
Frame_FileDialog()
{ super("带文件对话框的窗口");
setSize(60,70);
setVisible(true);
menubar=new MenuBar();
menu=new Menu("文件");
item=new MenuItem("运行可执行文件");
item.addActionListener(this);
menu.add(item);
menubar.add(menu);
setMenuBar(menubar);
filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD);
filedialog_load.setVisible(false);
filedialog_load.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{filedialog_load.setVisible(false);}
});
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{setVisible(false);System.exit(0);}
});
}
public void actionPerformed(ActionEvent e) //实现接口中的方法
{ if(e.getSource()==item)
{ filedialog_load.setVisible(true);
try
{File file=
new File(filedialog_load.getDirectory(),filedialog_load.getFile());
Runtime ce=Runtime.getRuntime();
ce.exec(file.toString());//把file用字符串表示,所有对象都能使用
//toString()
}
catch(FileNotFoundException e1){}
catch(IOException e2){}
}
}
}
20-例子9
import java.io.*;
public class Example20_9
{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("tom.dat","rw");}
catch(FileNotFoundException e){System.out.println("PPPP?????");}
try{for(int i=0;i<data.length;i++)
{in_and_out.writeInt(data[i]);
}
for(long i=data.length-1;i>=0;i--)//一个int型数据占4个字节,我们从
{in_and_out.seek(i*4); //文件的第36个字节读取最后面的一个整数,
System.out.print(","+in_and_out.readInt());//每隔4个字节往前读取一个整数。
}
in_and_out.close();
}
catch(IOException e){System.out.println("??????");}
}
}
20-例子10
import java.io.*;
class Example20_10
{ public static void main(String args[])
{ try{
RandomAccessFile file=new RandomAccessFile("Example20_10.java","rw");
long filePoint=0;
long fileLength=file.length();
while(filePoint<fileLength)
{String s=file.readLine();
System.out.println(s);
filePoint=file.getFilePointer();
}
file.close();
}
catch(Exception e){}
}
}
20-例子11
import java.io.*;
class Example20_11
{ public static void main(String args[])
{ PipedOutputStream out=null;
PipedInputStream in=null;
try
{ out=new PipedOutputStream();
in=new PipedInputStream();
in.connect(out);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -