How would I transfer 5-letter words into a new text file?












0















Let's say I have a txt file that has the whole dictionary in it. how would I make this code be able to transer only 5-letter words into a new created txt file?



 import java.io.*;
public class wordwebster {
public static void main(String args) throws IOException {
int five = 0;
File directory = new File(".");
String webster = directory.getCanonicalPath() + File.separator+ "webster.txt";
String fiveLetterWords = directory.getCanonicalPath()+ File.separator +"fiveLetterWords.txt";

File fin = new File(webster);
FileInputStream file = new FileInputStream(fin);
BufferedReader input = new BufferedReader(new InputStreamReader(file));

FileWriter fileStream = new FileWriter(fiveLetterWords,true);
BufferedWriter output = new BufferedWriter(fileStream);

String line = null;
while ((line = input.readLine())!= null){
output.write(line);
output.newLine();

}
input.close();

output.close();
}
}


EDIT:



As asked, let's say the input file (webster.txt) contain the words



Sentence
Frequent
Hello
Send
Variety
False


I would need only five letter words be extracted (Hello and False) and be put into a new file (fiveLetterWords.txt).










share|improve this question

























  • How does your input file contains words inside it? If each line contains only one word, you can filter words in each line else you may have to split the words in whole line. Can you add some sample lines from your input file?

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 12:38
















0















Let's say I have a txt file that has the whole dictionary in it. how would I make this code be able to transer only 5-letter words into a new created txt file?



 import java.io.*;
public class wordwebster {
public static void main(String args) throws IOException {
int five = 0;
File directory = new File(".");
String webster = directory.getCanonicalPath() + File.separator+ "webster.txt";
String fiveLetterWords = directory.getCanonicalPath()+ File.separator +"fiveLetterWords.txt";

File fin = new File(webster);
FileInputStream file = new FileInputStream(fin);
BufferedReader input = new BufferedReader(new InputStreamReader(file));

FileWriter fileStream = new FileWriter(fiveLetterWords,true);
BufferedWriter output = new BufferedWriter(fileStream);

String line = null;
while ((line = input.readLine())!= null){
output.write(line);
output.newLine();

}
input.close();

output.close();
}
}


EDIT:



As asked, let's say the input file (webster.txt) contain the words



Sentence
Frequent
Hello
Send
Variety
False


I would need only five letter words be extracted (Hello and False) and be put into a new file (fiveLetterWords.txt).










share|improve this question

























  • How does your input file contains words inside it? If each line contains only one word, you can filter words in each line else you may have to split the words in whole line. Can you add some sample lines from your input file?

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 12:38














0












0








0








Let's say I have a txt file that has the whole dictionary in it. how would I make this code be able to transer only 5-letter words into a new created txt file?



 import java.io.*;
public class wordwebster {
public static void main(String args) throws IOException {
int five = 0;
File directory = new File(".");
String webster = directory.getCanonicalPath() + File.separator+ "webster.txt";
String fiveLetterWords = directory.getCanonicalPath()+ File.separator +"fiveLetterWords.txt";

File fin = new File(webster);
FileInputStream file = new FileInputStream(fin);
BufferedReader input = new BufferedReader(new InputStreamReader(file));

FileWriter fileStream = new FileWriter(fiveLetterWords,true);
BufferedWriter output = new BufferedWriter(fileStream);

String line = null;
while ((line = input.readLine())!= null){
output.write(line);
output.newLine();

}
input.close();

output.close();
}
}


EDIT:



As asked, let's say the input file (webster.txt) contain the words



Sentence
Frequent
Hello
Send
Variety
False


I would need only five letter words be extracted (Hello and False) and be put into a new file (fiveLetterWords.txt).










share|improve this question
















Let's say I have a txt file that has the whole dictionary in it. how would I make this code be able to transer only 5-letter words into a new created txt file?



 import java.io.*;
public class wordwebster {
public static void main(String args) throws IOException {
int five = 0;
File directory = new File(".");
String webster = directory.getCanonicalPath() + File.separator+ "webster.txt";
String fiveLetterWords = directory.getCanonicalPath()+ File.separator +"fiveLetterWords.txt";

File fin = new File(webster);
FileInputStream file = new FileInputStream(fin);
BufferedReader input = new BufferedReader(new InputStreamReader(file));

FileWriter fileStream = new FileWriter(fiveLetterWords,true);
BufferedWriter output = new BufferedWriter(fileStream);

String line = null;
while ((line = input.readLine())!= null){
output.write(line);
output.newLine();

}
input.close();

output.close();
}
}


EDIT:



As asked, let's say the input file (webster.txt) contain the words



Sentence
Frequent
Hello
Send
Variety
False


I would need only five letter words be extracted (Hello and False) and be put into a new file (fiveLetterWords.txt).







java text






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 12:44







Joey Deguzman

















asked Nov 17 '18 at 12:31









Joey DeguzmanJoey Deguzman

356




356













  • How does your input file contains words inside it? If each line contains only one word, you can filter words in each line else you may have to split the words in whole line. Can you add some sample lines from your input file?

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 12:38



















  • How does your input file contains words inside it? If each line contains only one word, you can filter words in each line else you may have to split the words in whole line. Can you add some sample lines from your input file?

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 12:38

















How does your input file contains words inside it? If each line contains only one word, you can filter words in each line else you may have to split the words in whole line. Can you add some sample lines from your input file?

– Pushpesh Kumar Rajwanshi
Nov 17 '18 at 12:38





How does your input file contains words inside it? If each line contains only one word, you can filter words in each line else you may have to split the words in whole line. Can you add some sample lines from your input file?

– Pushpesh Kumar Rajwanshi
Nov 17 '18 at 12:38












1 Answer
1






active

oldest

votes


















1














If you need to allow only words whose length is exactly five, you can just put an if condition to check before writing into file. Modify your while loop to this,



while ((line = input.readLine()) != null) {
if (line.trim().length() == 5) {
output.write(line);
output.newLine();
}
}


Hope this helps. Let me know if you face any issues.






share|improve this answer



















  • 1





    This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

    – Joey Deguzman
    Nov 17 '18 at 13:26













  • Pleased to help :)

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 13:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351297%2fhow-would-i-transfer-5-letter-words-into-a-new-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you need to allow only words whose length is exactly five, you can just put an if condition to check before writing into file. Modify your while loop to this,



while ((line = input.readLine()) != null) {
if (line.trim().length() == 5) {
output.write(line);
output.newLine();
}
}


Hope this helps. Let me know if you face any issues.






share|improve this answer



















  • 1





    This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

    – Joey Deguzman
    Nov 17 '18 at 13:26













  • Pleased to help :)

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 13:27
















1














If you need to allow only words whose length is exactly five, you can just put an if condition to check before writing into file. Modify your while loop to this,



while ((line = input.readLine()) != null) {
if (line.trim().length() == 5) {
output.write(line);
output.newLine();
}
}


Hope this helps. Let me know if you face any issues.






share|improve this answer



















  • 1





    This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

    – Joey Deguzman
    Nov 17 '18 at 13:26













  • Pleased to help :)

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 13:27














1












1








1







If you need to allow only words whose length is exactly five, you can just put an if condition to check before writing into file. Modify your while loop to this,



while ((line = input.readLine()) != null) {
if (line.trim().length() == 5) {
output.write(line);
output.newLine();
}
}


Hope this helps. Let me know if you face any issues.






share|improve this answer













If you need to allow only words whose length is exactly five, you can just put an if condition to check before writing into file. Modify your while loop to this,



while ((line = input.readLine()) != null) {
if (line.trim().length() == 5) {
output.write(line);
output.newLine();
}
}


Hope this helps. Let me know if you face any issues.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 '18 at 13:16









Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

7,3382927




7,3382927








  • 1





    This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

    – Joey Deguzman
    Nov 17 '18 at 13:26













  • Pleased to help :)

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 13:27














  • 1





    This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

    – Joey Deguzman
    Nov 17 '18 at 13:26













  • Pleased to help :)

    – Pushpesh Kumar Rajwanshi
    Nov 17 '18 at 13:27








1




1





This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

– Joey Deguzman
Nov 17 '18 at 13:26







This has helped alot, however, I have changed the if line to only line.length since it would output the rest of the words but other than that thank you!

– Joey Deguzman
Nov 17 '18 at 13:26















Pleased to help :)

– Pushpesh Kumar Rajwanshi
Nov 17 '18 at 13:27





Pleased to help :)

– Pushpesh Kumar Rajwanshi
Nov 17 '18 at 13:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351297%2fhow-would-i-transfer-5-letter-words-into-a-new-text-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini