Creating new word document

Discussion in 'Java' started by srigattugari@gmail.com, Dec 28, 2006.

  1. Guest

    Hi all,
    Can you please help me out following issue.

    If i enter file name and filepath then a worddocument with the name as
    filename and that should be
    craete on specified path.How can i write java code for this.
    , Dec 28, 2006
    #1
    1. Advertising

  2. Guest

    hi,
    create an instance of java.io.File using the path as argument
    parameters. Then use the createNewFile(), I don't know the exact
    parameters but browse java.io.File and java.io.RandomAccessFile in the
    javadoc.
    , Dec 28, 2006
    #2
    1. Advertising

  3. wrote:
    ....
    > If i enter file name and filepath then a worddocument with the name as
    > filename and that should be
    > craete on specified path.How can i write java code for this.


    <sscce>
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;
    import java.io.*;

    class SaveDocType {
    public static void main(String[] args) {
    String fileType =
    (args.length==0 ?
    "doc" :
    args[0] );
    fileType = JOptionPane.showInputDialog(
    "enter desired file type", fileType );
    if (fileType==null) {
    System.out.println("Action cancelled by user");
    System.exit(0);
    }
    fileType = (fileType.startsWith(".") ?
    fileType.toLowerCase() :
    "." + fileType.toLowerCase() );
    JFileChooser saveFile = new JFileChooser(".");
    saveFile.setFileFilter(new FileTypeFilter(fileType));
    int returnVal = saveFile.showSaveDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
    File f = saveFile.getSelectedFile();
    String name = f.getName();
    if (!name.toLowerCase().endsWith( fileType )) {
    name = name + fileType;
    f = new File(f.getParent(), name);
    }
    System.out.println(
    "You chose to create the file: " + f);
    if ( f.exists() ) {
    System.out.println(
    "\n!! File not created !!");
    System.out.println(
    "Should not overwite existing file!: " + f);
    } else {
    try {
    boolean success = f.createNewFile();
    System.out.println(
    "Successfully created: " + f);
    } catch(IOException ioe) {
    ioe.printStackTrace();
    }
    }
    } else {
    System.out.println("Action cancelled by user");
    }
    }
    }

    class FileTypeFilter extends FileFilter {
    String type;

    FileTypeFilter(String fileType) {
    type = fileType;
    }

    public String getDescription() {
    return "File Type Filter";
    }

    public boolean accept(File f) {
    return f.getName().toLowerCase().endsWith(type);
    }
    }
    </sscce>

    OTOH, for actually putting *data* into the word
    document, you might look to Java POI - or a
    more sensible and generic format, such as
    HTML, or RTF.

    Andrew T.
    Andrew Thompson, Dec 28, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. goof1972
    Replies:
    0
    Views:
    6,574
    goof1972
    Dec 14, 2005
  2. srk
    Replies:
    0
    Views:
    618
  3. srk
    Replies:
    0
    Views:
    593
  4. Michael G. Schneider

    Modifying a Word document without using Word Automation

    Michael G. Schneider, Dec 15, 2003, in forum: ASP General
    Replies:
    5
    Views:
    270
    el.c. - myLittleTools.net
    Dec 16, 2003
  5. GaryB
    Replies:
    1
    Views:
    106
Loading...

Share This Page