关于javaosr的信息
本篇文章给大家谈谈javaosr,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java将eclipse上程序运行后的内容存入指定txt文件内?
- 2、用java设计一个电子记事本,要含保存,新建等基本功能,用EditPlus或Utredit,最好附带实验结果
- 3、如何用JAVA实现QuickSort排序
- 4、java中的InputStreamReader和OutputStreamWriter用法
- 5、java小程序编译成功却没有输出write方法写的呢日用
java将eclipse上程序运行后的内容存入指定txt文件内?
File fileout = new File("D:\\fw.txt");//这里改成你要写入的路径
OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream(fileout), "UTF-8");
BufferedWriter br = new BufferedWriter(osr);
br.write('里面加你的程序运行结果');
用java设计一个电子记事本,要含保存,新建等基本功能,用EditPlus或Utredit,最好附带实验结果
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Notepad implements ActionListener{
JFrame win;
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
FileOutputStream fos=null;
OutputStreamWriter osr=null;
Exitfounction ef=new Exitfounction();
JMenuItem saveItem,othersave,gb,gb2,ut,is;
JButton save;
JTextArea area;
File file;
String codetype=null;
static Font f=new Font("",Font.BOLD,15);
public Notepad(){
win=new JFrame();
area=new JTextArea();
area.setText("");
area.setFont(f);
/**主面板*/
JPanel main=new JPanel();
main.setLayout(new BorderLayout());
/**菜单面板*/
JMenuBar menuBar=createmenu();
/**中间面板*/
JPanel centerBar=new JPanel();
centerBar.setLayout(new BorderLayout());
/**工具兰面板*/
JToolBar function=function();
/**文本面板*/
JScrollPane textPanel=new JScrollPane();
/**面板布局*/
main.add(BorderLayout.NORTH,menuBar);
main.add(BorderLayout.CENTER,centerBar);
centerBar.add(BorderLayout.NORTH,function);
centerBar.add(BorderLayout.CENTER,textPanel);
textPanel.getViewport().add(area);
win.setContentPane(main);
addEventHandle();
}
private JToolBar function() {
JToolBar function=new JToolBar();
JButton newfile=new JButton("新建");
newfile.addActionListener(this);
// newfile.setFont(f);
save=new JButton("保存");
JButton print=new JButton("打印");
JButton backout=new JButton("撤销");
JButton copy=new JButton("复制");
JButton paste=new JButton("粘贴");
JButton find=new JButton("查找");
JButton replace=new JButton("替换");
function.add(newfile);
function.add(save);
function.add(print);
function.add(backout);
function.add(copy);
function.add(paste);
function.add(find);
function.add(replace);
return function;
}
private JMenuBar createmenu() {
JMenuBar menuBar=new JMenuBar();
JMenu file=new JMenu("文件");
JMenu editor=new JMenu("编辑");
JMenu see=new JMenu("查看");
JMenu search=new JMenu("搜索");
JMenu tool=new JMenu("工具");
JMenu doc=new JMenu("文档");
JMenu help=new JMenu("帮助");
JMenuItem newfile=new JMenuItem("新建");newfile.addActionListener(this);
JMenuItem open=new JMenuItem("打开");open.addActionListener(this);
othersave=new JMenuItem("另存为");
saveItem=new JMenuItem("保存");
JMenuItem exit=new JMenuItem("退出");
menuBar.add(file);
menuBar.add(editor);
menuBar.add(see);
menuBar.add(search);
menuBar.add(tool);
menuBar.add(doc);
menuBar.add(help);
file.add(newfile);
file.add(open);
file.add(saveItem);
file.add(othersave);
file.add(exit);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
ef.exit();
}
});
JMenuItem item;
editor.add(item=new JMenuItem("查找"));//item.addActionListener(this);
editor.add(item=new JMenuItem("替换"));//item.addActionListener(this);
editor.addSeparator();
editor.add(item=new JMenuItem("复制"));//item.addActionListener(this);
editor.add(item=new JMenuItem("粘贴"));//item.addActionListener(this);
editor.addSeparator();
editor.add(item=new JMenuItem("剪切"));//item.addActionListener(this);
editor.add(item=new JMenuItem("全选"));//item.addActionListener(this);
see.add(item=new JCheckBoxMenuItem("工具栏"));//item.addActionListener(this);
see.add(item=new JCheckBoxMenuItem("状态栏"));//item.addActionListener(this);
see.add(item=new JCheckBoxMenuItem("测边栏"));//item.addActionListener(this);
search.add(item=new JMenuItem("查找"));
search.add(item=new JMenuItem("查找上一个"));
search.add(item=new JMenuItem("查找下一个"));
search.add(item=new JMenuItem("替换"));
/**级联菜单*/
JMenu font=new JMenu("字体");//级联菜单
font.add(item=new JMenuItem("宋体"));//item.addActionListener(this);
font.add(item=new JMenuItem("楷体"));//item.addActionListener(this);
font.add(item=new JMenuItem("隶书"));//item.addActionListener(this);
font.add(item=new JMenuItem("黑体"));//item.addActionListener(this);
tool.add(font);
tool.add(item=new JMenuItem("颜色"));item.addActionListener(this);
tool.addSeparator();
ButtonGroup bg=new ButtonGroup();//按钮组 实现单选功能
tool.add(gb=new JRadioButtonMenuItem("GBK"));
bg.add(gb);gb.addActionListener(this);
tool.add(gb2=new JRadioButtonMenuItem("GB2312"));
bg.add(gb2);gb2.addActionListener(this);
tool.add(ut=new JRadioButtonMenuItem("UTF-8",true));
bg.add(ut);ut.addActionListener(this);
tool.add(is=new JRadioButtonMenuItem("ISO-8859-1"));
bg.add(is);is.addActionListener(this);
help.add(item=new JMenuItem("帮助....."));//item.addActionListener(this);
help.add(item=new JMenuItem("关于....."));//item.addActionListener(this);
return menuBar;
}
class Exitfounction{
public void exit(){
int ok=JOptionPane.showConfirmDialog(win, "Are you sure?","yes/no",JOptionPane.YES_NO_OPTION);
if(ok==JOptionPane.YES_NO_OPTION){
System.exit(0);
}else{win.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);}
}
}
private void addEventHandle(){
area.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent e) {
saveItem.setEnabled(true);
save.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
saveItem.setEnabled(true);
save.setEnabled(true);
}
public void removeUpdate(DocumentEvent e) {
saveItem.setEnabled(true);
save.setEnabled(true);
}});
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
saveItem.setEnabled(false);
save.setEnabled(false);
try {
fos=new FileOutputStream(file.getAbsolutePath());
osr=new OutputStreamWriter(fos);//通过自己手动设置编码来读取文件
osr.write(area.getText());
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(osr!=null)try{osr.close();}catch(Exception ee){}
if(fos!=null)try{fos.close();}catch(Exception ee){}
}
}});
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
saveItem.setEnabled(false);
save.setEnabled(false);
try {
fos=new FileOutputStream(file.getAbsolutePath());
osr=new OutputStreamWriter(fos);//通过自己手动设置编码来读取文件
osr.write(area.getText());
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(osr!=null)try{osr.close();}catch(Exception ee){}
if(fos!=null)try{fos.close();}catch(Exception ee){}
}
}});
othersave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
int n=jfc.showSaveDialog(win);//弹出对话框
if(n==JFileChooser.APPROVE_OPTION){
file=jfc.getSelectedFile();//得到被选中文件对象
try {
fos=new FileOutputStream(file.getAbsolutePath());
osr=new OutputStreamWriter(fos,codetype);//通过自己手动设置编码来读取文件
osr.write(area.getText());
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(osr!=null)try{osr.close();}catch(Exception ee){}
if(fos!=null)try{fos.close();}catch(Exception ee){}
}
}
}});
}
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str.equals("打开")){
area.setText("");
JFileChooser jfc=new JFileChooser();
int n=jfc.showOpenDialog(win);//弹出对话框
file=jfc.getSelectedFile();//得到被选中文件对象
if(file!=null){
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis);//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}
}if(str.equals("颜色")){
Color c=JColorChooser.showDialog(win, "选择文字颜色", Color.BLACK);//颜色选择
area.setForeground(c);
} if(gb.isSelected()){
codetype="GBK";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"GBK");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}else if(gb2.isSelected()){
codetype="GB2312";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"GB2312");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}else if(ut.isSelected()){
codetype="UTF-8";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"UTF-8");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}else if(is.isSelected()){
codetype="ISO-8859-1";
area.setText("");
try {
fis=new FileInputStream(file.getAbsolutePath());
isr=new InputStreamReader(fis,"ISO-8859-1");//可以通过自己手动设置编码来读取文件
br=new BufferedReader(isr);
String str1;
while((str1=br.readLine())!=null){//直接读取一行
area.append(str1+"\n");
}
} catch (Exception ee) {
ee.printStackTrace();
}finally{
if(br!=null)try{br.close();}catch(Exception ee){}
if(isr!=null)try{isr.close();}catch(Exception ee){}
if(fis!=null)try{fis.close();}catch(Exception ee){}
}
}
if(str.equals("新建")){
area.setText("");
}
}
public void showme(){
win.setSize(500,400);
win.setVisible(true);
win.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
ef.exit();
}
});
}
public static void main(String[] args) {
new Notepad().showme();
}
}
如何用JAVA实现QuickSort排序
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class Quick2 {
public static void main(String[] args) throws Exception {
File file = new File("D:\\num.txt");//这里的路径改成你的文本路径
File fileout = new File("D:\\fw.txt");
OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream(fileout), "UTF-8");
BufferedWriter br = new BufferedWriter(osr);
Scanner sc = new Scanner(new FileReader(file));
ArrayListInteger array = new ArrayListInteger();
while(sc.hasNext()){
array.add(Integer.valueOf(sc.nextLine().trim()));
}
System.out.println(array);
int a[] = new int[array.size()];
for(int i =0;iarray.size();i++){
a[i]=array.get(i);
}
_quickSort(a, 0, a.length-1);
System.out.println("\n\n");
for(int i=0;ia.length;i++){
br.write(String.valueOf(a[i]));//这里直接写入int类型的数总是乱码,所以干脆转换成String类型的写入 了
br.write("\r\n");
}
br.flush();
sc.close();
osr.close();
br.close();
}
public static int getMiddle(int[] a, int low, int high) {
int tmp = a[low]; //数组的第一个作为中轴
while (low high) {
while (low high a[high] = tmp) { //网上排序有问题,这里没写=写的导致存在重//复的数时便陷入死循环,我给改了下
high--;
}
a[low] = a[high]; //比中轴小的记录移到低端
while (low high a[low] tmp) {
low++;
}
a[high] = a[low]; //比中轴大的记录移到高端
}
a[low] = tmp; //中轴记录到尾
return low; //返回中轴的位置
}
public static void _quickSort(int[] a, int low, int high) {
if (low high) {
int middle = getMiddle(a, low, high); //将list数组进行一分为二
_quickSort(a, low, middle - 1); //对低字表进行递归排序
_quickSort(a, middle + 1, high); //对高字表进行递归排序
}
}
}
不好意思,昨晚没看清你的所有要求,漏了个输出。现在应该可以了,楼主试试吧,我写得也不容易,如果满意的话请采纳。
java中的InputStreamReader和OutputStreamWriter用法
java中OutputStreamWriter和Writer的区别还有InputStreamReader与Reader的区别如下:
1.OutputStreamWriter负责进行InputStream到Reader的适配和由OutputStream到Writer的适配。 Reader、Writer是用来处理16位元的流。
Reader支持16位的Unicode字符输出,InputStream支持8位的字符输出。
Reader和InputStream分别是I/O库提供的两套平行独立的等级机构,
InputStream、OutputStream是用来处理8位元的流,
2.java.io.Reader 和 java.io.InputStream 组成了 Java输入类。Reader 用于读入16位字符,也就是 Unicode编码的字符;而 InputStream 用于读入 ASCII字符和二进制数据。在 Java中,有不同类型的 Reader 输入流对应于不同的数据源:
FileReader 用于从文件输入;
CharArrayReader 用于从程序中的字符数组输入;
StringReader 用于从程序中的字符串输入;
PipedReader 用于读取从另一个线程中的 PipedWriter 写入管道的数据。
相应的也有不同类型的 InputStream 输入流对应于不同的数据源:FileInputStream,ByteArrayInputStream,StringBufferInputStream,PipedInputStream。另外,还有两种没有对应 Reader 类型的 InputStream 输入流
java小程序编译成功却没有输出write方法写的呢日用
去路径"C:/java/A/b/lol.txt" 好好找找。。。。。。。。找不着是肯定出错的了
javaosr的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javaosr的信息别忘了在本站进行查找喔。