如何检测我的程序在java中的用户

时间:2014-03-13 22:51:45

标签: java file

我正在使用文字处理器,我需要知道我的程序是什么用户,以便我的程序可以在启动时自动检测它并将文件保存在我的文档中。我正在使用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();

    }
}

1 个答案:

答案 0 :(得分:0)

假设您正在定位Windows(我的文档提示),您可以试试这个。

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