Вывод данных в файл

Модератор: Absurd

Ответить
zoja1739
Сообщения: 2
Зарегистрирован: 22 ноя 2009, 18:55

15 апр 2011, 12:11

Как зделать ,чтоб результат выодился в файл?

import java.util.Scanner;
import java.io.*;

import java.util.ArrayList;
import java.util.List;
public class Main{
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.println("vvesti stroku");
String str, g;
String a = new String();
str = in.nextLine();
byte brasymas[]=new byte[1000];
brasymas=str.getBytes();

try{
FileOutputStream fileOut=new FileOutputStream("text1.txt");
fileOut.write(brasymas);


}
catch(Exception e){
System.out.println(e);

}
finally{




in.close();

String[] words = str.split("\\s*;\\s*");
if (words.length == 0) {
System.err.println("No words found");
System.exit(1);
}
String firstWord = words[0];
Character centralChar = getCentralChar(firstWord);
if (centralChar == null) {
System.err.println("First word '" + firstWord + "' does not have central char");
System.exit(1);
}
char c = centralChar;
List<String> result = new ArrayList<String>();
for (String word : words) {
if (word.indexOf(c) != -1){
result.add(word);
}
}
System.out.println("result = " + result);
}
}
private static Character getCentralChar(String str) {
if (str == null || str.length() == 0 || str.length() % 2 == 0) {
return null;
}
return str.charAt(str.length() / 2);

}
}
DrLance
Сообщения: 27
Зарегистрирован: 17 июн 2005, 13:12
Откуда: spb
Контактная информация:

22 сен 2012, 20:51

import java.io.*;
public class WriteToFile {
public static String writeToFile(String filename, String strPage ){
PrintWriter writer = null;
try {
writer = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(filename), "windows-1251"));
writer.write(strPage);
writer.close();
} catch (Exception ex) {}
return strPage;
public static void main(String[] args) {
String test="test";
String filename="c:/filename";
writeToFile(filename, test );
}
вот пример. От него и пляшите.
Ответить