4


i am having a program in java.which system.out some strings,i need to save each of them in a text file
it is showing in a format
ruo1 row2 row3
i want it in
row1
row2
row3
how can i do that in java?

import java.util.Arrays;
import java.io.*;
public class BruteForce {
 public static FileOutputStream Output;
    public static PrintStream file;
    public static String line;

public static void main(String[] args) {
String password = "javabeanc";
char[] charset = "abcdefghijklmnopqrstuvwxyz".toCharArray();
BruteForce bf = new BruteForce(charset, 8);

String attempt = bf.toString();
while (true) {
    FileWriter writer;
    try {
      writer = new FileWriter("test.txt");

        writer.write(attempt+"\n");

      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }


attempt = bf.toString();
System.out.println("Tried: " + attempt);

bf.increment();
}
}


private char[] cs; // Character Set
private char[] cg; // Current Guess

public BruteForce(char[] characterSet, int guessLength) {
cs = characterSet;
cg = new char[guessLength];
Arrays.fill(cg, cs[0]);
}

public void increment() {
int index = cg.length - 1;
while(index >= 0) {
if (cg[index] == cs[cs.length-1]) {
if (index == 0) {
cg = new char[cg.length+1];
Arrays.fill(cg, cs[0]);
break;
} else {
cg[index] = cs[0];
index--;
}
} else {
cg[index] = cs[Arrays.binarySearch(cs, cg[index]) + 1];
break;
}
}
}

public String toString() {
return String.valueOf(cg);
}
}

4 Answers 4

6

Very quick code. I apologize if there are compile errors.

 import java.io.FileWriter;
  import java.io.IOException;  
  public class TestClass {
      public static String newLine = System.getProperty("line.separator");
      public static void main(String[] a) {
        FileWriter writer;
        try {
          writer = new FileWriter("test.txt");
          for(int i=0;i<3;i++){
            writer.write(row+i+newLine);
          }
          writer.close();
        } catch (IOException e) {
          e.printStackTrace();
        }

      }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

how about adding a new line character "\n" to each row ?

4 Comments

i need to save that to a file,above format is this is showing in my system.out.println,but i need to format it like Row1 Row2 Row3 in my file
Do you have source-level access to this particular program, or are you asking for general post-processing help? If the former, including a line feed character \n is appropriate for a Unix-based system. For Windows, use carriage-return line-feed as \r\n. If this is general post-processing help, you may not need a Java solution at all.
What code do you have that isn't producing the output that you want?
In your question you wrote about code that you have already written: "i am having a program in java.which system.out some strings". Then, in your first comment you wrote about the same code "above format is this is showing in my system.out.println". Show us THAT code.
0

u can use PrintWriter pw;
pw.println(row+i) in above instead of hard coding newLine

Comments

0

Using JDK 11 one can write:

public void writeToFile() {
    String content = "Line 1\nLine 2";
    Path path = Paths.get("./resources/sample-new.txt");
    Files.writeString(path, content);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.