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();
                }
        }

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.

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.