?
Solved

JAVA - Load string from file

Posted on 2009-07-12
8
Medium Priority
?
2,827 Views
Last Modified: 2013-11-21
HI, im trying to load words (censor) from a file

this is how i currently load it:
 public String badWords[] = {"xxx", "xxx", "xxx"};
and i can add more by just going ,"newword" inside the {}

but now i have lots of words, i need to load it from a file "badwords.cfg"

can someone provide some really simple code, that i can use?

by the way, i would like not to have to put a number of the amot, so i  can just add another without compiling...

THANKS
0
Comment
Question by:gamerx93
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 172 total points
ID: 24833787
You can keep the words line-by-line and use BufferedReader
to read line. Example here
 
http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html
0
 

Author Comment

by:gamerx93
ID: 24833790
yes, but then how do i load them into an array?
0
 
LVL 8

Assisted Solution

by:ozlevanon
ozlevanon earned 164 total points
ID: 24833930
Here is a sample code. It reads lines from a file, and puts each line in a set which is then converted to an array.

BufferedReader br = new BufferedReader(new FileReader("badwords.cfg"));
			Set<String> words = new HashSet<String>();
			while (true)
			{
				String line = br.readLine();
				if (line == null)
				{
					break;
				}
				words.add(line.trim());
			}
			br.close();
 
			String []array = new String[words.size()];
			words.toArray(array);
			System.out.println("array = " + Arrays.toString(array));

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:rnevet
ID: 24833990
If you don't need to edit the text file... the simplest way is using serialize.

http://www.java2s.com/Code/Java/Collections-Data-Structure/Serializingavector.htm
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 164 total points
ID: 24839460
The best way is just to treat the file as a List of String:

http://technojeeves.com/joomla/index.php/free/74-string-list
0
 
LVL 2

Expert Comment

by:guneshraj
ID: 24844245
     public static String getContents(File aFile) {
          StringBuffer contents = new StringBuffer();
          BufferedReader input = null;
          try {
                 InputStreamReader fr=new InputStreamReader(new FileInputStream(aFile),"UTF8");
                input = new BufferedReader( fr );
            String line = null; //not declared within while loop
            while (( line = input.readLine()) != null){
              contents.append(line);
              contents.append(System.getProperty("line.separator"));
            }
          }
          catch (FileNotFoundException ex) {
            //ex.printStackTrace();
          }
          catch (IOException ex){
            //ex.printStackTrace();
          }
          finally {
            try {
              if (input!= null) {
                input.close();
              }
            }
            catch (IOException ex) {
              //ex.printStackTrace();
            }
          }
          return contents.toString();
        }

--
//{"xxx", "xxx", "xxx"}; to String Array
String words = getContents(new File("badwords.cfg"));
// Now remove all the control chars
words = words.replaceAll("{", "");
words = words.replaceAll("};", "");
words = words.replaceAll("\"", "");
words = words.replaceAll("\n", "");
// All the chars that are not part of the plan

String badwords[] = words.split("[,]");

** There you go, Hope this example helps.

Gunesh





0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24844309
You don't need to read line by line actually

String[] badWords = fileToString("badwords.cfg").split("[\r\n]");

See

http://technojeeves.com/joomla/index.php/free/70-reading-a-file-into-a-string-in-java
0

Featured Post

Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question