2014-03-13 30 views
2

我正在使用文字处理器,我需要知道用户程序是什么,以便我的程序可以在启动时自动检测到它并将文件保存在我的文档中。我使用java。并且我没有目录检测的预编码。但如果这有助于这里是我的代码:如何检测我的程序在java中的用户

import javax.swing.*; 

import java.io.*; 
import java.awt.event.*; 
import java.util.*; 


@SuppressWarnings("serial") 
class Word1 extends JFrame implements ActionListener, KeyListener{ 
    ClassLoader Idr = this.getClass().getClassLoader(); 
    static JPanel pnl = new JPanel(); 
    public static JTextField Title = new JTextField("Document",25); 
    static JTextArea Body = new JTextArea("Body", 48, 68); 
    public static JScrollPane pane = new JScrollPane(Body); 
    JTextField textField = new JTextField(); 
    JButton saveButton = new JButton("Save File"); 
    JButton editButton = new JButton("Edit File"); 
    JButton deleteButton = new JButton("Delete"); 
    public static String Input = ""; 
    public static String Input2 = ""; 
    public static String Text = ""; 
    public static void main(String[] args){ 
     @SuppressWarnings("unused") 
     Word1 gui = new Word1(); 
    } 
    public void ListB(){ 
     // Directory path here 
     String username = JOptionPane.showInputDialog(getContentPane(), "File to Delete", "New ", JOptionPane.PLAIN_MESSAGE); 
     String path = "C:\\Users\\"+username+"\\Documents\\"; 
     File folder = new File(path); 
     File[] listOfFiles = folder.listFiles(); 

     for (int i = 0; i < listOfFiles.length; i++) 
     { 

      if (listOfFiles[i].isFile()) 
      { 
       String files = listOfFiles[i].getName(); 
       if (files.endsWith(".txt") || files.endsWith(".TXT")){ 
        ArrayList<String> doclist = new ArrayList<String>(); 
        doclist.add(files); 
        System.out.print(doclist); 
        String l = doclist.get(i); 
       } 

      } 
     } 
    } 
    public Word1() 
    { 
     ListB(); 
     setTitle("Ledbetter Word"); 
     setSize(800, 850); 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     add(pnl); 
     Body.setLineWrap(true); 
     Body.setWrapStyleWord(true); 
     Body.setEditable(true); 
     Title.setEditable(true); 
     Title.addKeyListener(keyListen2); 
     saveButton.addActionListener(this); 
     editButton.addActionListener(len); 
     deleteButton.addActionListener(len2); 
     pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     Body.addKeyListener(keyListen); 
     pnl.add(saveButton); 
     pnl.add(editButton); 
     pnl.add(deleteButton); 
     pnl.add(Title); 
     pnl.add(pane); 
     setVisible(true); 
    } 
    KeyListener keyListen = new KeyListener() { 
     @Override 
     public void keyTyped(KeyEvent e) { 
      @SuppressWarnings("unused") 
      char keyText = e.getKeyChar(); 
     } 

     @Override 
     public void keyPressed(KeyEvent e) { 

     }//ignore 

     @Override 
     public void keyReleased(KeyEvent e) { 
      Word1.Input = Body.getText(); 
     }//ignore 
    }; 
    KeyListener keyListen2 = new KeyListener() { 
     @Override 
     public void keyTyped(KeyEvent e) { 
      @SuppressWarnings("unused") 
      char keyText = e.getKeyChar(); 

     } 

     @Override 
     public void keyPressed(KeyEvent e) { 

     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      Word1.Input2 = Title.getText(); 
     } 
    }; 
    public void keyTyped(KeyEvent e) { 

    } 

    public void keyPressed(KeyEvent e) { 

    } 

    public void keyReleased(KeyEvent e) { 

    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource() == saveButton){ 

      try { 
       BufferedWriter fileOut = new BufferedWriter(new FileWriter("C:\\Users\\David\\Documents\\"+Title.getText()+".txt")); 
       fileOut.write(Word1.Input); 
       fileOut.close(); 
       JOptionPane.showMessageDialog(getParent(), "Successfully saved to your documents", "Save", JOptionPane.INFORMATION_MESSAGE); 
      } catch (IOException ECX) { 
       JOptionPane.showMessageDialog(getParent(), "There was an error saving your file", "Save-Error", JOptionPane.ERROR_MESSAGE); 
      } 

     } 
    } 
    ActionListener len = new ActionListener(){ 
     @SuppressWarnings({ "resource" }) 
     public void actionPerformed(ActionEvent e){ 
      if(e.getSource() == editButton);{ 
       String filename = JOptionPane.showInputDialog(getParent(), "File to Delete", "Deletion", JOptionPane.PLAIN_MESSAGE); 
       Word1.Body.setText(""); 
       try{ 
        FileReader file = new FileReader(filename+".txt"); 
        BufferedReader reader = new BufferedReader(file); 
        String Text = ""; 
        while ((Text = reader.readLine())!= null) 
        { 
         Word1.Body.append(Text+"\n"); 
        } 
        Word1.Title.setText(filename); 
       } catch (IOException e2) { 
        JOptionPane.showMessageDialog(getParent(), "Open File Error\nFile may not exist", "Error!", JOptionPane.ERROR_MESSAGE); 
       } 
      } 

     } 
    }; 

    ActionListener len2 = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if(e.getSource() == deleteButton);{ 
       String n = JOptionPane.showInputDialog(getParent(), "File to Delete", "Deletion", JOptionPane.PLAIN_MESSAGE); 
       Delete(new File(n+".txt")); 
      } 
     } 
    }; 
    public void Delete(File Files){ 
     Files.delete(); 

    } 
} 
+0

标准问题:你有什么试过? –

+1

呃,为什么?只需使用'System.getProperty(“user.home”)',这是用户的主目录 – fge

+0

如果操作系统没有接口直接保存到用户的“我的文档”文件夹中,而不必使用这样的绝对路径。 –

回答

0

假设你是针对Windows(我的文档提示),你可以试试这个。

String myDocumentsPath = System.getProperty("user.home") + "/Documents";

+0

感谢哈伦!那就是诀窍。 – Morpheus

相关问题