我用的eclipse,選中項(xiàng)目,右鍵-export-java-doc-next就這樣就導(dǎo)出了,導(dǎo)出的是網(wǎng)頁形式的注釋說明文檔,想要導(dǎo)出doc格式的文檔那是不行的。
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括寶清網(wǎng)站建設(shè)、寶清網(wǎng)站制作、寶清網(wǎng)頁制作以及寶清網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,寶清網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到寶清省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
package com.test01;
import java.util.Scanner;
public class oop5 { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int x = 9;
// int y = 1;
int x = sc.nextInt();
int y = sc.nextInt();
int z;
z = add(x, y);
System.out.println("x的值為:" + x);
System.out.println("y的值為:" + y);
System.out.println("二者之和為:" + z);
}
/** 四種小算法 */
// 加法運(yùn)算
public static int add(int a, int b) {
int c;
c = a + b;
return c;
}
// 減法運(yùn)算
public static int jian(int d, int v) {
int m;
m = d - v;
return m;
}
// 乘積運(yùn)算
public static int addAdd(int q, int w) {
int e;
e = q * w;
return e;
}
// 除法運(yùn)算
public static int chu(int p, int k) {
int f;
f = p / k;
return f;
}
}
給你個(gè)做好了的Java的源程序的記事本,自己看看就行了的,不怎么難的···
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)于對話框");
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();
}
}
java項(xiàng)目中文件的路徑-方法大全
一、 相對路徑的獲得
說明:相對路徑(即不寫明時(shí)候到底相對誰)均可通過以下方式獲得(不論是一般的java項(xiàng)目還是web項(xiàng)目)
System.getProperty("user.dir");
上述相對路徑中,java項(xiàng)目中的文件是相對于項(xiàng)目的根目錄web項(xiàng)目中的文件路徑視不同的web服務(wù)器不同而不同(tomcat是相對于tomcat安裝目錄\bin)
二 類加載目錄的獲得(即當(dāng)運(yùn)行時(shí)某一類時(shí)獲得其裝載目錄)
1.1)通用的方法一(不論是一般的java項(xiàng)目還是web項(xiàng)目,先定位到能看到包路徑的第一級目錄)
InputStreamis=TestAction.class.getClassLoader().getResourceAsStream("test.txt");?(test.txt文件的路徑為 項(xiàng)目名\src\test.txt;類TestPath所在包的第一級目錄位于src目錄下)
上式中將TestPath,test.txt替換成對應(yīng)成相應(yīng)的類名和文件名字即可
1.2)通用方法二?(此方法和1.1中的方法類似,不同的是此方法必須以'/'開頭) InputStream is=Test1.class.getResourceAsStream("/test.txt");?
(test.txt文件的路徑為 項(xiàng)目名\src\test.txt,類Test1所在包的第一級目錄位于src目錄下)
三 web項(xiàng)目根目錄的獲得(發(fā)布之后)
1 從servlet出發(fā)
可建立一個(gè)servlet在其的init方法中寫入如下語句(沒有請求的話會(huì)拋空指針導(dǎo)常)
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (關(guān)鍵)?
結(jié)果形如:F:\tomcat-6.0.36\webapps\test\(test為項(xiàng)目名字)
如果是調(diào)用了s1.getRealPath("")則輸出F:\tomcat-6.0.36\webapps\test(少了一個(gè)"\")
2 從httpServletRequest出發(fā)(沒有請求的話會(huì)拋空指針導(dǎo)常)
String path=request.getSession().getServletContext().getRealPath("/");
結(jié)果形如:?F:\tomcat-6.0.36\webapps\test\
四 classpath的獲取(在Eclipse中為獲得src或者classes目錄的路徑),放在監(jiān)聽器,可以窗口啟動(dòng)獲取路徑
方法一?Thread.currentThread().getContextClassLoader().getResource("").getPath()
String path = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
System.out.println("path========"?+ path);輸出:?path========/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/
方法二?JdomParse.class.getClassLoader().getResource("").getPath()?(JdomParse為src某一個(gè)包中的類,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
輸出:JdomParse.class.getClassLoader().getResource-/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/
另外,如果想把文件放在某一包中,則可以 通過以下方式獲得到文件(先定位到該包的最后一級目錄)
eg String p2=JdomParse.class.getResource("").getPath();?
System.out.println("JdomParse.class.getResource---"+p2);
輸出:JdomParse.class.getResource--/F:/tomcat-6.0.36/webapps/test/WEB-INF/classes/
(JdomParse為src目錄下jdom包中的類)
四 屬性文件的讀取:
方法 一
InputStream in = lnewBufferedInputStream(?new?FileInputStream(name));?
Properties p =?new?Properties();?p.load(in);
注意路徑的問題,做執(zhí)行之后就可以調(diào)用p.getProperty("name")得到對應(yīng)屬性的值
方法二
Locale locale =Locale.getDefault();?
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest",locale);?
String value = localResource.getString("test");?
System.out.println("ResourceBundle: " + value);
工程src目錄下propertiesTest.properties(名字后綴必須為properties)文件內(nèi)容如下:
test=hello word
不通過Servlet獲取路徑
第一種實(shí)現(xiàn)
Java代碼
URL url = ClassLoader.getSystemClassLoader().getResource("./");
File file =new?File(url.getPath());
File parentFile =new?File(file.getParent());
System.out.println("webRoot:"+parentFile.getParent());
第二種實(shí)現(xiàn)?
首先寫一個(gè)接聽類 (推薦使用,容器啟動(dòng)時(shí)就執(zhí)行,不會(huì)拋空指針異常,適合做定時(shí)器任務(wù)來刪除服務(wù)器文件的路徑)
Java代碼:
package com.chinacreator.report.listener;?
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @authorxiaoqun.yi
*/
public class PathListener implementsServletContextListener {
private staticServletContext servletContext;
public voidcontextDestroyed(ServletContextEvent sce) {
this.servletContext= sce.getServletContext();
System.out.println("path=======:"+servletContext.getRealPath("/"));
}
public voidcontextInitialized(ServletContextEvent arg0) {
}
}
在web.xml中加入如下配置
Java代碼 :
listener
listener-classcom.chinacreator.report.listener.PathListener/listener-class
/listener
五、Java中的getResourceAsStream有以下幾種:?
1. Class.getResourceAsStream(String path) : path 不以’/'開頭時(shí)默認(rèn)是從此類所在的包下取資源,以’/'開頭則是從ClassPath根下獲取。其只是通過path構(gòu)造一個(gè)絕對路徑,最終還是由 ClassLoader(類加載器)(獲取資源)
2. Class.getClassLoader.getResourceAsStream(String path) :默認(rèn)則是從ClassPath根下獲取,path不能以’/'開頭,最終是由ClassLoader獲取資源。
3. ServletContext. getResourceAsStream(String path):默認(rèn)從WebAPP根目錄下取資源,Tomcat下path是否以’/'開頭無所謂,當(dāng)然這和具體的容器實(shí)現(xiàn)有關(guān)。
4. Jsp下的application內(nèi)置對象就是上面的ServletContext的一種實(shí)現(xiàn)。?
其次,getResourceAsStream 用法大致有以下幾種:?
第一: 要加載的文件和.class文件在同一目錄下,例如:com.x.y 下有類me.class ,同時(shí)有資源文件myfile.xml?
那么,應(yīng)該有如下代碼:?
me.class.getResourceAsStream("myfile.xml");?
第二:在me.class目錄的子目錄下,例如:com.x.y 下有類me.class ,同時(shí)在 com.x.y.file 目錄下有資源文件myfile.xml?
那么,應(yīng)該有如下代碼:?
me.class.getResourceAsStream("file/myfile.xml");?
第三:不在me.class目錄下,也不在子目錄下,例如:com.x.y 下有類me.class ,同時(shí)在 com.x.file 目錄下有資源文件myfile.xml
那么,應(yīng)該有如下代碼:?
me.class.getResourceAsStream("/com/x/file/myfile.xml");?
總結(jié)一下,可能只是兩種寫法?
第一:前面有 “?? / ”?
“ / ”代表了工程的根目錄,例如工程名叫做myproject,“ / ”代表了myproject?
me.class.getResourceAsStream("/com/x/file/myfile.xml");?
第二:前面沒有 “?? / ”?
代表當(dāng)前類的目錄?
me.class.getResourceAsStream("myfile.xml");?
me.class.getResourceAsStream("file/myfile.xml");
新聞標(biāo)題:java項(xiàng)目代碼微盤 java項(xiàng)目代碼下載
鏈接地址:http://muchs.cn/article24/doecdce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、手機(jī)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)