java - java中通过mac终端操作文件

标签 java terminal text-files

我正在开发一个程序,该程序接受 .doc 或 .pdf 文件并将其转换为 .txt。从那里,我使用 java 扫描仪来读取文件并对其进行我喜欢的操作。为了转换 .doc 和 .pdf,我使用 Runtime.getRuntime().exec("textutil") 来转换文件。

但是,当我尝试转换名称中包含空格的文件时,就会出现问题。它基本上需要一个包含段落问题和答案的文件,然后一次一个句子地向您读回它们,这并不完全优雅。这是代码:

//it has to do with the space in the file name, i suppose i could just rename each file i plan on using
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;

public class AClass
{
    public static void main(String[] args)
    {
        String fileName="/Users/name/Documents/Quizzes/Finals 1.doc"; // a default string
        try
        {
        String s="textutil -convert txt "+correct(fileName);    //see function correct
        System.out.println(s);    //prints the string, if I copy and paste it into terminal it DOES execute
        Process proc=Runtime.getRuntime().exec(s);         //executes in terminal, don't look into this... it works
    } catch (Exception e) { System.out.println("Conversion failed");}
    File file=new File(fileName);
    Scanner reader=null;
    Scanner keyboard=new Scanner(System.in);
    try
    {
        reader=new Scanner(file);
    }
    catch (FileNotFoundException e)
    {
        System.out.println(e.getMessage());
        System.out.println("Save the file to "+fileName);
        System.exit(0);
    }
    System.out.println("Found the file!");
    System.out.println("Hit enter to read each sentence (otherwise it will quit). \nThe program will notify you when the answer is next\n");

    int qCount=0;                       //just a counter
    while (reader.hasNext())       //not eof
    {
        String line=reader.nextLine().trim();
        String[] sentences;
        //System.out.println(line);
        sentences=breakUp(line);     //break up the entire line into sentences[], this //function DOES work.
        for (int i=0; i<sentences.length; i++)
        {
            String s="";
            if (!(line.equals("") || line.equals("\n")))
            {
                s= keyboard.nextLine();  //hit enter to see each sentence, this loop works
            }
            if (!s.equals("")) //quit on non-null input
            {
                System.out.println("Done");
                System.exit(0);
            }
            if (sentences[i].toLowerCase().indexOf("answer") != -1)     //if answer is in the sentence
            {
                System.out.println("\nThe answer is next, hit enter again to see it");
                keyboard.nextLine();
                System.out.println(sentences[i]);
                qCount++;
            }
            else
            {
                int max=120;                   //simple formatting (output window doesn't //auto \n for lines)
                if (sentences[i].length()<max)
                    System.out.println(sentences[i]);
                else
                {
                    for (int j=0; j<sentences[i].length(); j+=max)
                    {
                        if (j+max>sentences[i].length())
                            System.out.println(sentences[i].substring(j, sentences[i].length()));
                        else
                            System.out.println(sentences[i].substring(j, j+max));
                    }
                }
            }
        }
    }

    System.out.println("End of file");
    System.out.println("Total questions="+qCount);
}

public static String[] breakUp(String line)      //this function works, finds periods
{
    if ((line.equals("") || line.equals(null)) || line.length()<2)
        return new String[] {""};
    String[] tempSents=new String[500];
    int count=0;
    int pos=0;
    int dotPos=line.indexOf(".", pos);
    while (dotPos != -1 && count<tempSents.length)
    {
        tempSents[count]=line.substring(pos, dotPos+1);
        pos=dotPos+1;
        dotPos=line.indexOf(".", pos);
        count++;
    }
    if (count==0)
        return new String[] {line};
    else
    {
        tempSents[count]=line.substring(pos);
        count++;
    }
    return Arrays.copyOf(tempSents, count);
}

public static String correct(String s)   //this function works, it adds a '\' in front of //a space so that when it is passed to the terminal it is proper syntax
{
    for (int i=0; i<s.length(); i++)
    {
        if (s.charAt(i)==' ')
        {
            s=s.substring(0, i)+"\\"+s.substring(i);
            i++;
            if (i<=s.length())
                return s.trim();
        }
    }
    return s.trim();
}
}

同样,当我打印出传递给 exec 的字符串并将其复制并粘贴到终端时,它确实正确执行,但是通过运行时命令没有任何反应(对于名称中带有空格的文件,否则它会工作) 。 提前致谢

最佳答案

尝试转义文件名中的空格。使用类似 "file\\name.doc" 您可以优化正确的(字符串)方法:string.replaceAll("", "\\");

关于java - java中通过mac终端操作文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15980800/

相关文章:

ruby - 如何使用 ANSI 转义序列在 bash 中捕获终端窗口的标题?

powershell从文本文件中导入字符串后的数据

java - 我可以将 NIO2 文件路径用于非本地文件系统架构吗

java 7 到 java 8 迁移 spring bean 创建异常

java - 在 Android 中上传图像和文本文件时上传视频不起作用

java - 如何在参数对象类中使用@CookieValue?

java - 通过命令行编译但不是在 ant 中

XCode 项目配置列表

java - 如何将包含整数和字符串的文本文件读入数组

php - 使用 php 显示 .txt 文件的内容