2017-12-11 17 views
2

동시에 모니터로 인쇄하는 동안 파일로 인쇄하려고합니다. 그건 그렇고, 그것은 모니터에 완벽하게 잘 인쇄합니다. 내 코드는 파일을 생성하지 않습니다. 그것은 내 코드의 다른 부분에 파일을 생성했기 때문에 매우 혼란 스럽습니다.파일과 모니터를 동시에 인쇄하는 방법은 무엇입니까?

나는 solution이 내 문제와 비슷하지만 FileOutputStream 또는 PrintStream에 대해 알지 못한다는 것을 알고 있습니다. 나는 코딩 수업에 대한 소개를하고 수업에서 배운 것을 사용할 수 있습니다. 이것을 이루기위한 다른 방법이 있습니까?

아래 코드는 제 코드입니다.

public static void printLine(char n, int count) throws IOException //prints lines of chord 
{ 


FileWriter file = new FileWriter("outFile.txt",true); 
PrintWriter outputfile = new PrintWriter(file); 
//File outFile = new File("outFile.txt"); 

char[] lineNames = {'f','d','B','G','E','C'}; 
char[] spaceNames = {'e','c','A','F','D'}; 

char[] lineAr = {'-','-',n,'-','-'}; //array to print lines 
char[] spaceAr = {' ',' ',n,' ',' '}; //array to print spaces 


//print line 
if(n == lineNames[count]) 
{ 
    lineAr[2] = n; 
} 
else 
    lineAr[2] = '-'; 
for(int x = 0; x<5; x++) 
{ 
    System.out.print(lineAr[x]); //prints to monitor 
    outputfile.print(lineAr[x]); //prints to output file 
}//end for loop 
System.out.println(); 

//print space 
if(n == spaceNames[count]) 
{ 
    spaceAr[2] = n; 
} 
else 
    spaceAr[2] = ' '; 
for(int x = 0; x<5; x++) 
{ 
    System.out.print(spaceAr[x]); 
    outputfile.print(lineAr[x]); 
}//end for loop 
System.out.println(); 

//print line C 
if(n == 'C') 
{ 
    lineAr[2] = n; 
} 
else 
    lineAr[2] = '-'; 
for(int x = 0; x<5; x++) 
{ 
    System.out.print(lineAr[x]); 
    outputfile.print(lineAr[x]); 
}//end for loop 
System.out.println(); 

outputfile.close(); 
}//end printLine method 

수동으로 파일을 만들려고했지만 해당 파일에 인쇄 할 코드가 있지만 파일이 비어 있습니다.

+0

가능성을 [콘솔 및 텍스트 파일에 쓰기] (https://stackoverflow.com/questions/16237546/writing-to-console-and-text-file) – vinS

+0

감사합니다. FileOutputStream에 대해 배웠고 클래스에서 배운 내용 만 사용할 수 있습니다. 이것을 이루기위한 다른 방법이 있습니까? @vinS –

답변

1

것은 내가이 파일에 쓰기뿐만 아니라 콘솔에 인쇄됩니다

FileWriter writer = new FileWriter("testfile.txt"); 
    for(int i = 0; i < 10; i++){ 
     System.out.println("a"); 
     writer.append("a"); 
    } 

    writer.close(); 

이 작동 희망 대신 print

for(int x = 0; x<5; x++) 
{ 
    System.out.print(lineAr[x]); //prints to monitor 
    outputfile.write(lineAr[x]); //write to output file 
}//end for loop 
System.out.println(); 
+0

안녕하세요, 답변 해 주셔서 감사합니다. 불행히도 쓰기가 작동하지 않습니다. –

+0

그것은 나를 위해 완벽하게 작동했습니다. –

0

write()를 사용해보십시오 - 'testfile.txt