954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Binary logs in LINUX

Hello all

i have created a java application which is writing to log files to specific path.logs are created successfully.When i use cat command to show the contents it is showing the correct data but when i use vi to open the file it is showing the contents like binary files.

When i use file command to check the type of file it is showing that it is a binary file while i wanted to create a text file.Code is as follows

public static void logResponse(ArrayList list,String LOG_PATH)
        {
                System.out.println("Response list is"+list.toString());
                String date=now();
                String Filename=date+"_LogResponse.txt";
                System.out.println("Filename is"+Filename);
                File req=new File(LOG_PATH,Filename);
                System.out.println(req.toString());

                                                writeLog(list,req);
        }
          public static void writeLog(ArrayList list,File file)
        {
                String str="";
                        int counter=0,j=1;
                        if (list.isEmpty() == false) {
                        Iterator objIterator = list.iterator();
                        Object[] obj=list.toArray();
                        for(counter=0;counter<obj.length;counter++)
                        {
                        if(counter==10*j)
                        {
                                str=str.concat("\n");
                                str=str+(String)obj[counter];
                                j+=1;
                        }
                        else if(counter==0)
                        {
                                str=str+(String)obj[counter];
                        }
                        else
                        str=str+"|"+(String)obj[counter];
                        }

                        System.out.println(str);
                        }

                try
                 {
                        boolean success=file.createNewFile();
                        if(success)
                        {
                                fos = new FileOutputStream(file);
                                dos=new DataOutputStream(fos);
                                int i=0;
                                dos.writeChars(str);
                                dos.writeChars("\n");
                        }
                        else
                        {
                                fos = new FileOutputStream(file,true);
                                dos=new DataOutputStream(fos);
                                dos.writeChars(str);
                                dos.writeChars("\n");
                        }
                }
                catch(IOException io)
                {
                                io.printStackTrace();
                }
        }
manish250
Junior Poster in Training
87 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

Why are you using DataOutputStream for writing out textual content? Use PrintWriter by passing in the FileOutputStream created, invoke its println() method and you should be good to go.

Always make sure that you use XxxWriter for writing out textual content rather than streams.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: