简体   繁体   中英

Save a http link in word document with java BufferedWriter

i want to save a http link in word document using java application (file.doc) .i succesfully write the link to the word document using BufferedWriter class but the link in word document is just simple line of text . if you press enter button on the document word it turns to http link and that's what i want to do with my java application , save as http format

here is my code :

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.JTextField;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Save_file extends JFrame {

    private JPanel contentPane;
    public static JTextField textField;
    JFileChooser chooser;
    File file;
    public static FileWriter fw;
    public static BufferedWriter output;
    String the_past;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Save_file frame = new Save_file();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Save_file() {
        setResizable(false);

        setIconImage(Toolkit.getDefaultToolkit().getImage(Save_file.class.getResource("/Buttons/LogoIcon.png")));
        setTitle("Saving file");
        this.setSize(435, 300);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //  setBounds(100, 100, 435, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("Select a File :");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblNewLabel.setBounds(12, 13, 152, 25);
        contentPane.add(lblNewLabel);

        JButton btnNewButton = new JButton("");
        btnNewButton.setIcon(new ImageIcon(Save_file.class.getResource("/Buttons/loaddisk (2).gif")));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                try{


                    output = new BufferedWriter(fw);
                    output.write(textField.getText()+"\n");
                    output.write(" ");
                    JOptionPane.showMessageDialog(null, "Saving succes", "Save", JOptionPane.INFORMATION_MESSAGE);
                    //textField.setText("");
                    output.write("\r");
                    output.flush();
                    output.close();


                }
                catch(Exception io){
                    JOptionPane.showMessageDialog(null, "Saving Failed", "Error", JOptionPane.ERROR_MESSAGE);
                }

            }
        });
        btnNewButton.setBounds(142, 211, 32, 32);
        contentPane.add(btnNewButton);

        JLabel lblLink = new JLabel("Link   ");
        lblLink.setToolTipText("Just click once to past");
        lblLink.setFont(new Font("Tahoma", Font.BOLD, 13));
        lblLink.setBounds(12, 107, 97, 25);
        contentPane.add(lblLink);

        textField = new JTextField();
        textField.setToolTipText("Just click once to past");
        textField.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                the_past= textField.getText();
                if (the_past.isEmpty()){
                textField.paste();
                }
                //else
                    //textField.setText("");
            }
        });
        textField.setColumns(10);
        textField.setBounds(12, 154, 393, 33);
        contentPane.add(textField);

        JButton btnOpenFile = new JButton("Open File");
        btnOpenFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

               try{

                    chooser = new JFileChooser();
                    int retval = chooser.showOpenDialog(Save_file.this);


                    if (retval == JFileChooser.APPROVE_OPTION) {
                        //... The user selected a file, get it, use it.
                        File file = chooser.getSelectedFile();
                        System.out.println("File equals"+file);
                        fw = new FileWriter(file, true);


                    }


                }
                catch(Exception io){


                }
            }

        });
        btnOpenFile.setBounds(12, 53, 97, 25);
        contentPane.add(btnOpenFile);

        JButton button = new JButton("");
        button.setIcon(new ImageIcon(Save_file.class.getResource("/Buttons/cc.jpg.png")));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        button.setBounds(243, 211, 35, 35);
        contentPane.add(button);
    }
}

保存示例链接的结果结果应该是

No, you are not doing that.

You are writting a text (ASCII or UTF) file with the String that you use, and assign a .doc extension to the file. A .doc extension does not make your file a word document . MS Word can read automatically text files and show it, but you are not writting (I repeat) a Word file.

Search for a API that allows manipulating/creating Word files to do that (I think Apache has one of them).

As it has already been stated, you create a simple plain-text document, which doesn't support hyperlinks. You should take a look at Apache POI library to create a Rich Text Format document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM