Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JG-90 Mostipan Reverse file .java #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hillel.elementary.javageeks.mostipan.reverse;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class Reverse {
public static void main(String[] args) throws Exception {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't use main methods

URL from = Reverse.class.getClassLoader().getResource("Test.kt");
File toFile = new File("/home/sanya/hillel/java-geeks/modules/mostipan/src/main/resources/Reversed.kt");
refactor(from.getFile(), toFile.getAbsolutePath());
}

public static void refactor(String pathToFileFrom, String pathToFileTo) throws Exception {
BufferedReader fileIn = null;
BufferedWriter fileOut = null;
try {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use try catch with resources or close streams manually

fileIn = new BufferedReader(new InputStreamReader(new FileInputStream(pathToFileFrom), StandardCharsets.UTF_8));
fileOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToFileTo), StandardCharsets.UTF_8));
String buffer;

while ((buffer = fileIn.readLine()) != null) {
StringBuilder out = new StringBuilder(buffer);
out.reverse();
fileOut.write(out.toString());
if (buffer == null) {
break;
}


}
} catch (Exception e) {
e.printStackTrace();
}
}
}