Usando FileWriter para imprimir una nueva línea

This is a method to encrypt/decrypt a file(SourceFile.txt) and save the result in another file(ConvertedFile.txt)

private void crypto(int index, int k)   throws IOException// 0 -> encrypt       1 -> decrypt
{
    FileReader in;
    FileWriter out;
    try
    {
        in = new FileReader("SourceFile.txt");
        out = new FileWriter("ConvertedFile.txt");
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File NOT Found.");
        return;
    }
    c = new cipher(k);
    Scanner sc = new Scanner(in);
    String line,token;
    StringTokenizer st;
    while(sc.hasNextLine())
    {
        line = sc.nextLine();
        st = new StringTokenizer(line," ");
        while(st.hasMoreTokens())
        out.write(c.convert(st.nextToken(),index)+" ");
        out.write(10);
    }
    in.close();
    out.close();
}

If the sourceFile has something in it like

María tenía

un poco

cordero

The convertedFile contains the encryption as

Qevc leh e pmxxpi peqf

Is there anyway how I can use the FileWriter to print a '\n'(new line) at the end of every line?

I even tried the out.write(10)... didn't work

preguntado el 27 de noviembre de 13 a las 07:11

Wild guess: You're on windows and should write \r\n -

Wow... that actually worked.... Thank you... -

@SBI, 10 is the ASCII value of newline \n -

@Masud Of course it is. But Windows uses a carriage return & new line combination \r\n, whereas unix uses only a newline \n. This is legacy handling of how white space works, going back to old typewriters. -

4 Respuestas

Use PrintWriter

PrintWriter p = new PrintWriter(out);
p.println(str);

contestado el 26 de mayo de 16 a las 20:05

Puedes envolver el FileWriter en un parche de BufferedWriter y use newLine()

BufferedWriter bw = new BufferedWriter(new FileWriter("text.txt"));

bw.write(line);
bw.newLine();

respondido 27 nov., 13:07

You missed braces of while loop.

while(sc.hasNextLine())
{
    line = sc.nextLine();
    st = new StringTokenizer(line, " ");

    while(st.hasMoreTokens()) 
    {   // Add braces here for newline
        out.write(c.convert(st.nextToken(), index) + " ");
        out.write(10);  //Without braces it will be skipped.
    }
}

respondido 27 nov., 13:07

If I include these braces, every word is gonna be printed in a separate line.... I don't want that.... - VivekGhosh

        FileWriter out = null;
        try {
            out = new FileWriter("F:/work1.txt");
            out.write("Rakesh");
            out.write("\r\n");
            out.write("KR");
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Then the file contains:

Rakesh
KR

Or You Can Use BufferedWriter as

public void fileWrite(String fileName,ArrayList<String> str){
        File           f ;
        FileWriter     fw = null;
        BufferedWriter bw = null;
        try {
            f  = new File(fileName);
            fw = new FileWriter(f.getAbsoluteFile());
            bw = new BufferedWriter(fw);
            for(String i:str){
                bw.write(i);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(bw!=null)
                    bw.close();
                if(fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }   
    }

respondido 27 nov., 13:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.