本站消息

  出租广告位,需要合作请联系站长

  今日名言-想象你自己对困难作出的反应,不是逃避或绕开它们,而是面对它们,同它们打交道,以一种进取的和明智的方式同它们奋斗 。——马克斯威尔·马尔兹

  今日名言-用谅解、宽恕的目光和心理看人、待人。人就会觉得葱笼的世界里,春意盎然,到处充满温暖。——蔡文甫


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

初学者:java练习题———含答案------输入输出流(6)

发布于2021-07-18 20:34     阅读(603)     评论(0)     点赞(24)     收藏(2)


在这里插入图片描述
在这里插入图片描述

import java.io.*;

public class Keyboard {

	public static void main(String[] args) throws IOException { 
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);

		FileOutputStream fos = new FileOutputStream("myfile.txt");
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		BufferedWriter bw = new BufferedWriter(osw);
		
		System.out.println("请输入字符串(按Ctrl+Z结束):");
		String str = null;	
		while ((str = br.readLine()) != null) {
			bw.write(str); 	
			bw.write("\n");	
		}
		br.close();
		bw.close();		
		System.out.println("创建文件完毕!");
	}
}

运行结果为
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

import java.io.*;

public class TextFileCopy {

	public static void main(String[] args) {
		try {
			FileReader input = new FileReader(args[0]);
			BufferedReader br = new BufferedReader(input);
			
			FileWriter output = new FileWriter(args[1]);
			BufferedWriter bw = new BufferedWriter(output);
			
			String str = br.readLine();
			while (str != null) {
				bw.write(str);
				bw.newLine(); 	
				str = br.readLine();
			}
			bw.flush();
			bw.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		} catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		System.out.println("文件复制完毕!");
	}

}

此处问题有些多,捣鼓了我快一个小时,总结的一些小东西

1.如何给 main 函数中的 args[ ] 字符串数组赋值
在这里插入图片描述
这种情况就是你没有给字符串数组赋值

我转载别人的,一搜就有,一眼就能看懂,根据软件不同

idea的
https://blog.csdn.net/David_jiahuan/article/details/90780518

eclipse的
https://blog.csdn.net/wangyin970774934/article/details/52252669

2.系统提示找不到指定的文件
在这里插入图片描述
这个时候不能简单的输入你所创建的 类名.java 了

要输入根路径,即你所创建的文件在的具体位置

最简单的方式就是你将你所创建的 .java 文件直接复制某个盘上
此时的根路径就是C:\xxxx(你创建类的名字).java

你也可以直接找到你 .java 的路径,然后直接复制一大串
但是如果你的根路径中有
在这里插入图片描述
由此符号的,他会报错并在此处停下。

最后运行结果为
在这里插入图片描述
然后你找到你的 .txt 打开后即为
在这里插入图片描述
在这里插入图片描述

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
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.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

class MyMenuBar extends MenuBar {
	private static final long serialVersionUID = 1L;

	public MyMenuBar(Frame parent) {
		parent.setMenuBar(this);
	}

	public void addMenus(String[] menus) {
		for (int i = 0; i < menus.length; i++)
			add(new Menu(menus[i]));
	}

	public void addMenuItems(int menuNumber, String[] items) {
		for (int i = 0; i < items.length; i++) {
			if (items[i] != null)
				getMenu(menuNumber).add(new MenuItem(items[i]));
			else
				getMenu(menuNumber).addSeparator();
		}
	}

	public void addActionListener(ActionListener al) {
		for (int i = 0; i < getMenuCount(); i++)
			for (int j = 0; j < getMenu(i).getItemCount(); j++)
				getMenu(i).getItem(j).addActionListener(al);
	}
}

class MyFile {
	private FileDialog fDlg;

	public MyFile(Frame parent) {
		fDlg = new FileDialog(parent, "", FileDialog.LOAD);
	}

	private String getPath() {
		return fDlg.getDirectory() + "\\" + fDlg.getFile();
	}

	public String getData() throws IOException {
		fDlg.setTitle("打开");
		fDlg.setMode(FileDialog.LOAD);
		fDlg.setVisible(true);
		BufferedReader br = new BufferedReader(new FileReader(getPath()));
		StringBuffer sb = new StringBuffer();
		String aline;
		while ((aline = br.readLine()) != null)
			sb.append(aline + '\n');
		br.close();
		return sb.toString();
	}

	public void setData(String data) throws IOException {
		fDlg.setTitle("保存");
		fDlg.setMode(FileDialog.SAVE);
		fDlg.setVisible(true);
		BufferedWriter bw = new BufferedWriter(new FileWriter(getPath()));
		bw.write(data);
		bw.close();
	}
}

class MyClipboard {
	private Clipboard cb;

	public MyClipboard() {
		cb = Toolkit.getDefaultToolkit().getSystemClipboard();
	}

	public void setData(String data) {
		cb.setContents(new StringSelection(data), null);
	}

	public String getData() {
		Transferable content = cb.getContents(null);
		try {
			return (String) content.getTransferData(DataFlavor.stringFlavor);
		} catch (Exception ue) {
		}
		return null;
	}
}

class MyFindDialog extends Dialog implements ActionListener {
	private static final long serialVersionUID = 1L;
	private Label lFind = new Label("查找字符串:");
	private Label lReplace = new Label("替换字符串:");
	private TextField tFind = new TextField(10);
	private TextField tReplace = new TextField(10);
	private Button bFind = new Button("查找");
	private Button bReplace = new Button("替换");
	private TextArea ta;

	public MyFindDialog(Frame owner, TextArea ta) {
		super(owner, "查找", false);
		this.ta = ta;
		setLayout(null);
		lFind.setBounds(10, 30, 80, 20);
		lReplace.setBounds(10, 70, 80, 20);
		tFind.setBounds(90, 30, 90, 20);
		tReplace.setBounds(90, 70, 90, 20);
		bFind.setBounds(190, 30, 80, 20);
		bReplace.setBounds(190, 70, 80, 20);
		add(lFind);
		add(tFind);
		add(bFind);
		add(lReplace);
		add(tReplace);
		add(bReplace);
		setResizable(false);
		bFind.addActionListener(this);
		bReplace.addActionListener(this);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				MyFindDialog.this.dispose();
			}
		});
	}

	public void showFind() {
		setTitle("查找");
		setSize(280, 60);
		setVisible(true);
	}

	public void showReplace() {
		setTitle("查找替换");
		setSize(280, 110);
		setVisible(true);
	}

	private void find() {
		String text = ta.getText();
		String str = tFind.getText();
		int end = text.length();
		int len = str.length();
		int start = ta.getSelectionEnd();
		if (start == end)
			start = 0;
		for (; start <= end - len; start++) {
			if (text.substring(start, start + len).equals(str)) {
				ta.setSelectionStart(start);
				ta.setSelectionEnd(start + len);
				return;
			}
		}
		ta.setSelectionStart(end);
		ta.setSelectionEnd(end);
	}

	private void replace() {
		String str = tReplace.getText();
		if (ta.getSelectedText().equals(tFind.getText()))
			ta.replaceRange(str, ta.getSelectionStart(), ta.getSelectionEnd());
		else
			find();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == bFind)
			find();
		else if (e.getSource() == bReplace)
			replace();
	}
}

public class MyMemo extends Frame implements ActionListener {
	private static final long serialVersionUID = 1L;
	private TextArea editor = new TextArea();
	private MyFile mf = new MyFile(this);
	private MyClipboard cb = new MyClipboard();
	private MyFindDialog findDlg = new MyFindDialog(this, editor);

	public MyMemo(String title) {
		super(title);
		MyMenuBar mb = new MyMenuBar(this);
		mb.addMenus(new String[] { "文件", "编辑", "查找", "帮助" });
		mb.addMenuItems(0, new String[] { "新建", "打开", "保存", null, "退出" });
		mb.addMenuItems(1, new String[] { "剪切", "复制", "粘贴", "清除", null, "全选" });
		mb.addMenuItems(2, new String[] { "查找", null, "查找替换" });
		mb.addMenuItems(3, new String[] { "我的记事本信息" });
		add(editor);
		mb.addActionListener(this);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				MyMemo.this.dispose();
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		String selected = e.getActionCommand();
		if (selected.equals("新建"))
			editor.setText("");
		else if (selected.equals("打开")) {
			try {
				editor.setText(mf.getData());
			} catch (IOException ie) {
			}
		} else if (selected.equals("保存")) {
			try {
				mf.setData(editor.getText());
			} catch (IOException ie) {
			}
		} else if (selected.equals("退出")) {
			dispose();
		} else if (selected.equals("剪贴")) {
			cb.setData(editor.getSelectedText());
			editor.replaceRange("", editor.getSelectionStart(), editor.getSelectionEnd());
		} else if (selected.equals("复制")) {
			cb.setData(editor.getSelectedText());
		} else if (selected.equals("粘贴")) {
			String str = cb.getData();
			editor.replaceRange(str, editor.getSelectionStart(), editor.getSelectionEnd());
		} else if (selected.equals("清除")) {
			editor.replaceRange("", editor.getSelectionStart(), editor.getSelectionEnd());
		} else if (selected.equals("全选")) {
			editor.setSelectionStart(0);
			editor.setSelectionEnd(editor.getText().length());
		} else if (selected.equals("查找")) {
			findDlg.showFind();
		} else if (selected.equals("查找替换")) {
			findDlg.showReplace();
		}
	}

	public static void main(String[] args) {
		MyMemo memo = new MyMemo("我的记事本");
		memo.setSize(300, 300);
		memo.setVisible(true);
	}
}

在这里插入图片描述
运行结果为
在这里插入图片描述

原文链接:https://blog.csdn.net/basinle/article/details/117871151



所属网站分类: 程序员的那点事

作者:风雨雷电

链接:http://www.pythonpdf.com/blog/article/97/7d2a25daaaab8d2ccaae/

来源:编程知识网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

24 0
收藏该文
已收藏

评论内容:(最多支持255个字符)