給你個(gè)做好了的Java的源程序的記事本,自己看看就行了的,不怎么難的···
專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)桂林免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新記事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("開始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替換為");
private JButton jba=new JButton("全部替換");
private Icon ic=new ImageIcon("D:\\java課堂筆記\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替換");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新記事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打開".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
if(flag){
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語句,保證了程序的健壯性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤銷".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=jta.getText();
jta.cut();
}else if("復(fù)制".equals(comm)){
jta.copy();
}else if("粘貼".equals(comm)){
value=jta.getText();
jta.paste();
}else if("刪除".equals(comm)){
value=jta.getText();
jta.replaceSelection(null);
}else if("全選".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替換".equals(comm)){
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找內(nèi)容:");
JLabel jl2=new JLabel("替換為:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"關(guān)于對(duì)話框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
// jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("開始".equals(comm)||"下一個(gè)".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jb.setText("下一個(gè)");
// value=value.substring(s+temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完畢!", "提示", 0, ic);
jf.dispose();
}
}else if("替換為".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完畢!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替換".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp+new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","編輯","幫助"};
String[][] menuItemLab={{"新建","打開","保存","另存","退出"},
{"撤銷","剪切","復(fù)制","粘貼","刪除","全選","查找","替換"},
{"版本"}};
for(int i=0;imenuLab.length;i++){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j++){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自動(dòng)換行
JScrollPane jsp=new JScrollPane(jta);//滾動(dòng)窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
import java.math.BigDecimal;
public class Test{
public static void main(String[] args){
BigDecimal bd1 = new BigDecimal(10000000000000000d);
BigDecimal bd2 = new BigDecimal(90000000000000000d);
BigDecimal sum = bd1.add(bd2);
long doublesum = sum.longValue();//轉(zhuǎn)為long類型
System.out.println(sum);//打印BigDecimal類型值
System.out.println(doublesum);//打印long類型值
}
}
你這句話的意思是每次讀取一個(gè)字節(jié),將讀取的字節(jié)強(qiáng)轉(zhuǎn)成byte類型放入數(shù)組中,也就是在數(shù)組相應(yīng)的位置放入每次讀取的數(shù)據(jù)。如果len的長(zhǎng)度超過數(shù)組長(zhǎng)度的話是會(huì)包異常的??梢赃@樣寫:
String s = "";
while((temp=in.read(b))!=-1)
{
s+=new String(b);
}
system.out.print(s);
這樣就不會(huì)超出數(shù)組長(zhǎng)度了,這個(gè)是每次讀取1024個(gè)字節(jié)放入數(shù)組中,滿了之后就執(zhí)行循環(huán)內(nèi)的語句,繼續(xù)循環(huán)時(shí)b中的內(nèi)容就會(huì)自動(dòng)清空。手打,希望采納!!
不知道是否理解對(duì)了你的意思,大概寫了一下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("處理完畢");
}
/**
* 文本文件轉(zhuǎn)換為指定編碼的字符串
*
* @param file
* 文本文件
* @param encoding
* 編碼類型
* @return 轉(zhuǎn)換后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 將輸入流寫入輸出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回轉(zhuǎn)換結(jié)果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 將字符串寫入指定文件(當(dāng)指定的父路徑中文件夾不存在時(shí),會(huì)最大限度去創(chuàng)建,以保證保存成功!)
*
* @param res
* 原字符串
* @param filePath
* 文件路徑
* @return 成功標(biāo)記
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符緩沖區(qū)
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
upload是定義的一個(gè)list,里邊放的是file類型的對(duì)象,循環(huán)取出每一個(gè)file對(duì)象,然后把每一個(gè)file對(duì)象輸出到“d:\upload\文件名”目錄下,并將file對(duì)象中的內(nèi)容輸出到新生成文件中去。其實(shí)可以看做是一個(gè)文件copy的功能。
實(shí)例化一個(gè)byte類型的數(shù)組而已,用來存放讀取出來的file內(nèi)容,然后將這個(gè)數(shù)組的內(nèi)容在讀入到d盤下你設(shè)定的目錄中去。
import java.security.*;
import javax.crypto.*;
/**
* 本例解釋如何利用DES私鑰加密算法加解密
*
* @author Devon
* @version 1.0 04/03/10
*/
public class SingleKeyExample {
public static void main(String[] args) {
try {
String algorithm = "DES"; //定義加密算法,可用 DES,DESede,Blowfish
String message = "Hello World. 這是待加密的信息";
// 生成個(gè)DES密鑰
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
keyGenerator.init(56); //選擇DES算法,密鑰長(zhǎng)度必須為56位
Key key = keyGenerator.generateKey(); //生成密鑰
// 生成Cipher對(duì)象
Cipher cipher = Cipher.getInstance("DES");
//用密鑰加密明文(message),生成密文(cipherText)
cipher.init(Cipher.ENCRYPT_MODE, key); //操作模式為加密(Cipher.ENCRYPT_MODE),key為密鑰
byte[] cipherText = cipher.doFinal(message.getBytes()); //得到加密后的字節(jié)數(shù)組
System.out.println("加密后的信息: " + new String(cipherText));
//用密鑰加密明文(plainText),生成密文(cipherByte)
cipher.init(Cipher.DECRYPT_MODE, key); //操作模式為解密,key為密鑰
byte[] sourceText = cipher.doFinal(cipherText); //獲得解密后字節(jié)數(shù)組
System.out.println("解密后的信息: " + new String(sourceText));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* @author Devon
*/
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
public class PairKeyExample {
public static void main(String argv[]) {
try {
String algorithm = "RSA"; //定義加密算法,可用 DES,DESede,Blowfish
String message = "張三,你好,我是李四";
//產(chǎn)生張三的密鑰對(duì)(keyPairZhang)
KeyPairGenerator keyGeneratorZhang =
KeyPairGenerator.getInstance(algorithm); //指定采用的算法
keyGeneratorZhang.initialize(1024); //指定密鑰長(zhǎng)度為1024位
KeyPair keyPairZhang = keyGeneratorZhang.generateKeyPair(); //產(chǎn)生密鑰對(duì)
System.out.println("生成張三的公鑰對(duì)");
// 張三生成公鑰(publicKeyZhang)并發(fā)送給李四,這里發(fā)送的是公鑰的數(shù)組字節(jié)
byte[] publicKeyZhangEncode = keyPairZhang.getPublic().getEncoded();
//通過網(wǎng)絡(luò)或磁盤等方式,把公鑰編碼傳送給李四
//李四接收到張三編碼后的公鑰,將其解碼
KeyFactory keyFacoryLi = KeyFactory.getInstance(algorithm); //得到KeyFactory對(duì)象
X509EncodedKeySpec x509KeySpec =
new X509EncodedKeySpec(publicKeyZhangEncode); //公鑰采用X.509編碼
PublicKey publicKeyZhang = keyFacoryLi.generatePublic(x509KeySpec); //將公鑰的KeySpec對(duì)象轉(zhuǎn)換為公鑰
System.out.println("李四成功解碼,得到張三的公鑰");
//李四用張三的公鑰加密信息,并發(fā)送給李四
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); //得到Cipher對(duì)象
cipher.init(Cipher.ENCRYPT_MODE, publicKeyZhang); //用張三的公鑰初始化Cipher對(duì)象
byte[] cipherMessage = cipher.doFinal(message.getBytes()); //得到加密信息
System.out.println("加密后信息:" + new String(cipherMessage));
System.out.println("加密完成,發(fā)送給李四...");
//張三用自己的私鑰解密從李四處收到的信息
cipher.init(Cipher.DECRYPT_MODE, keyPairZhang.getPrivate()); //張三用其私鑰初始化Cipher對(duì)象
byte[] originalMessage = cipher.doFinal(cipherMessage); //得到解密后信息
System.out.println("張三收到信息,解密后為:" + new String(originalMessage));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}