<commit_before>package org.wikiutils;

import java.io.*;
import java.util.*;
import java.nio.file.*;

import org.wikipedia.Wiki;

/**
 * Contains several read-write operations.
 * 
 * @author Fastily
 * 
 * @see org.wikiutils.CollectionUtils
 * @see org.wikiutils.GUIUtils
 * @see org.wikiutils.LoginUtils
 * @see org.wikiutils.ParseUtils
 * @see org.wikiutils.StringUtils
 * @see org.wikiutils.WikiUtils
 */
public class IOUtils
{
	/**
	 * Hiding constructor from JavaDoc
	 */
	private IOUtils(){}

	
	/**
	 *  Creates a HashMap from a file. Key and Value must be separated by 
         *  colons. One newline per entry. Example line: "KEY:VALUE". Useful for
         *  storing deletion/editing reasons. The text file to read from must be
         *  in UTF-8 encoding.
	 * 
	 *  @param path The path of the file to read from
	 *  @throws IOException if a filesystem error occurs
	 *  @return The HashMap we created by parsing the file
	 */
	public static HashMap<String, String> buildReasonCollection(String path) throws IOException
	{
            HashMap<String, String> l = new HashMap<>();
            Files.readAllLines(Paths.get(path)).forEach(line ->
            {
                line = line.trim();
                int i = line.indexOf(":");
                l.put(line.substring(0, i), line.substring(i + 1));
            });
            return l;
	}
	
	/**
	 * Loads list of pages contained in a file into an array. One newline per item.
	 * 
	 * @param file The abstract filename of the file to load from.
	 * @param prefix Append prefix/namespace to items? If not, specify <tt>""</tt>.
	 * @param encoding The text encoding of the file to load from.
	 * 
	 * @return The resulting array
	 * 
	 * @throws FileNotFoundException If the specified file was not found.
         * @deprecated replace with {@code Files.lines(path, charset).map(line -> prefix + line.trim()).collect(Collectors.joining("\n"));}
	 */
        @Deprecated
	public static String[] loadFromFile(String file, String prefix, String encoding) throws FileNotFoundException
	{
		Scanner m = new Scanner(new File(file), encoding);
		ArrayList<String> l = new ArrayList<String>();
		while (m.hasNextLine())
			l.add(prefix + m.nextLine()/*.trim()*/);
		return l.toArray(new String[0]);
	}
	
	
	/**
	 * Copies a file.
	 * 
	 * @param src The path of the source file
	 * @param dest The location to copy the file to.
	 * 
	 * @throws FileNotFoundException If the source file could not be found
	 * @throws IOException If we encountered some sort of read/write error
         * @deprecated use <code>Files.copy(Paths.get(src), Paths.get(dest))</code><commit_msg>Undo custom changes in deprecated IOUtils.loadFromFile helper function<commit_after>package org.wikiutils;

import java.io.*;
import java.util.*;
import java.nio.file.*;

import org.wikipedia.Wiki;

/**
 * Contains several read-write operations.
 * 
 * @author Fastily
 * 
 * @see org.wikiutils.CollectionUtils
 * @see org.wikiutils.GUIUtils
 * @see org.wikiutils.LoginUtils
 * @see org.wikiutils.ParseUtils
 * @see org.wikiutils.StringUtils
 * @see org.wikiutils.WikiUtils
 */
public class IOUtils
{
	/**
	 * Hiding constructor from JavaDoc
	 */
	private IOUtils(){}

	
	/**
	 *  Creates a HashMap from a file. Key and Value must be separated by 
         *  colons. One newline per entry. Example line: "KEY:VALUE". Useful for
         *  storing deletion/editing reasons. The text file to read from must be
         *  in UTF-8 encoding.
	 * 
	 *  @param path The path of the file to read from
	 *  @throws IOException if a filesystem error occurs
	 *  @return The HashMap we created by parsing the file
	 */
	public static HashMap<String, String> buildReasonCollection(String path) throws IOException
	{
            HashMap<String, String> l = new HashMap<>();
            Files.readAllLines(Paths.get(path)).forEach(line ->
            {
                line = line.trim();
                int i = line.indexOf(":");
                l.put(line.substring(0, i), line.substring(i + 1));
            });
            return l;
	}
	
	/**
	 * Loads list of pages contained in a file into an array. One newline per item.
	 * 
	 * @param file The abstract filename of the file to load from.
	 * @param prefix Append prefix/namespace to items? If not, specify <tt>""</tt>.
	 * @param encoding The text encoding of the file to load from.
	 * 
	 * @return The resulting array
	 * 
	 * @throws FileNotFoundException If the specified file was not found.
         * @deprecated replace with {@code Files.lines(path, charset).map(line -> prefix + line.trim()).collect(Collectors.joining("\n"));}
	 */
        @Deprecated
	public static String[] loadFromFile(String file, String prefix, String encoding) throws FileNotFoundException
	{
		Scanner m = new Scanner(new File(file), encoding);
		ArrayList<String> l = new ArrayList<String>();
		while (m.hasNextLine())
			l.add(prefix + m.nextLine().trim());
		return l.toArray(new String[0]);
	}
	
	
	/**
	 * Copies a file.
	 * 
	 * @param src The path of the source file
	 * @param dest The location to copy the file to.
	 * 
	 * @throws FileNotFoundException If the source file could not be found
	 * @throws IOException If we encountered some sort of read/write error
         * @deprecated use <code>Files.copy(Paths.get(src), Paths.get(dest))</code>
{'fraction_non_alphanumeric': 0.11211267605633803, 'fraction_numerical': 0.0015023474178403756, 'mean_word_length': 2.9018315018315017, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1759697', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1502, 'n_words': 538}