Note that there are some explanatory texts on larger screens.

plurals
  1. POManipulating files through the mac terminal in java
    primarykey
    data
    text
    <p>I am working on a program that takes a .doc or .pdf file and converts it into a .txt. From there, I use the java scanner to read the file and do with it what i please. In order to convert the .doc and .pdf I use the <code>Runtime.getRuntime().exec("textutil")</code> to convert the files.</p> <p>However, the problem arises when I try to convert a file that contains a space in its name. It basically takes a file with paragraph questions and answers, and reads them back to you one sentence at a time, not entirely elegant. Here is the code:</p> <pre><code>//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&lt;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()&lt;max) System.out.println(sentences[i]); else { for (int j=0; j&lt;sentences[i].length(); j+=max) { if (j+max&gt;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()&lt;2) return new String[] {""}; String[] tempSents=new String[500]; int count=0; int pos=0; int dotPos=line.indexOf(".", pos); while (dotPos != -1 &amp;&amp; count&lt;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&lt;s.length(); i++) { if (s.charAt(i)==' ') { s=s.substring(0, i)+"\\"+s.substring(i); i++; if (i&lt;=s.length()) return s.trim(); } } return s.trim(); } } </code></pre> <p>Again, when i print out the string that i pass into exec and copy and paste it into the terminal, it does execute properly, however nothing happens through the runtime command (for files with space in the name, it works otherwise). Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload