Skip to content

Instantly share code, notes, and snippets.

@ableasdale
Last active September 10, 2015 11:10
Show Gist options
  • Save ableasdale/f758610aa47f5b06f18e to your computer and use it in GitHub Desktop.
Save ableasdale/f758610aa47f5b06f18e to your computer and use it in GitHub Desktop.
URLEncode multiple lines in a text file (for use with JMeter)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* Created with IntelliJ IDEA.
* User: Alex
* Date: 10/09/15
* Time: 10:36
*/
public class UrlEncodeTextFile {
public static void main(String[] args) {
String folderPath = "D:\\data";
Path source = Paths.get(folderPath, "source.txt");
Path dest = Paths.get(folderPath, "dest.txt");
Charset charset = Charset.forName("UTF-8");
try {
BufferedWriter writer = Files.newBufferedWriter(dest, charset, StandardOpenOption.CREATE);
BufferedReader reader = Files.newBufferedReader(source, charset);
String line = null;
while ((line = reader.readLine()) != null) {
String ul = URLEncoder.encode(line, "UTF-8") + "\n";
writer.write(ul, 0, ul.length());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment