Hi there;
There is such a simple code here, and with the propotion of its simplicity, it drives me crazy.
Here is the code:

private void fillArrayLists(String fileName,ArrayList <String> list)
	{

		try{

			// Open the file that is the first 
			
			// command line parameter
			
			FileInputStream fstream = new FileInputStream(fileName);
			
			// Get the object of DataInputStream
		 
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			//Read File Line By Line
			
			while ((strLine = br.readLine()) != null)   {
				// Print the content on the console
				System.out.println (strLine.trim());
				list.add(strLine.trim());
				
			}
			//Close the input stream
			in.close();
		    
		}catch (Exception e){//Catch exception if any
		
			System.err.println("Error: " + e.getMessage());
		  	
		}
		  
	}

It simply reads the input and fills proper arraylist.
I call them such like that:

System.out.println("*** RANKS: ***");
		fillArrayLists("ranks.txt" , this.ranks);
		System.out.println("*** Departments: ***");
		fillArrayLists("departments.txt" , this.departments );
		System.out.println("*** names: ***");
		fillArrayLists("names.txt", this.names);
		System.out.println("*** surnames: ***");
		fillArrayLists("surnames.txt", this.surnames);

RANKS printed such as:
*** RANKS: ***
��t��u��g��g��e��n��e��a��l instead of "tuggeneral"
surnames are bad as mentioned above. But names and departments are printed as expected.

Why?

Recommended Answers

All 2 Replies

What kind of file are you reading? How was it created?
It looks like it contains Unicode characters with a leading id character. Here is the contents of a file in hex that I created with Wordpad by selecting Unicode Text Document as the Save type:
FFFE540068006900730020006900730020006100200074006500730074002E00

Notice there is a 00 every other pair of digits except at the start.

Thanks for your reply.
I've read txt files, and I've changed them as a UTF-8. Problem solved such a way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.